[英]Extra Tkinter GUI popup
我已經寫了一堆生成GUI的代碼。 現在,每當我運行代碼時,它都會產生主GUI窗口和一個沒有任何內容的附加小窗口。 當我關閉較小的窗口時,大型主窗口消失了。 現在,我一直在閱讀其他類似問題的文章,但是我無法確定錯誤在代碼中的位置。
請幫忙 :)
跟進問題:如何添加背景圖像而不是無聊的灰色?
#%% GUI Interface
import Tkinter as tk
from tkFont import Font
from PIL import ImageTk, Image
from Tkinter import END
#This creates the main window of an application
window = tk.Toplevel()
window.title("Sat Track")
window.geometry("1200x800")
window.configure(background='#f0f0f0')
#Imports the pictures.
pic1 = "Globeview.png"
pic2 = "MercatorView.png"
pic3 = "currentweathercroppedsmall.png"
pic4 = "GECurrentcroppedsmall.png"
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img1 = ImageTk.PhotoImage(Image.open(pic1))
img2 = ImageTk.PhotoImage(Image.open(pic2))
img3 = ImageTk.PhotoImage(Image.open(pic3))
img4 = ImageTk.PhotoImage(Image.open(pic4))
header = tk.Label(window, text="Satellite Control Center", font=Font(size=40))
header.pack()
toprow = tk.Frame(window)
infobox = tk.Text(toprow, width=50, height=7, font=("Calibri",12))
infobox.pack(side = "left")
infobox.insert(END,"Current information for:"+spacer+name +'\n'+
"Time:" +space+times+ '\n'+
"Longitude:"+space +x_long+ '\n'+
"Latitude:" +space+x_lat+ '\n'+
"Altitude:" +space+alt+space+ "[km]"+'\n'+
"Velocity:" +space+vel+space+ "[km/s]" + '\n'+
"Spatial Resolution: "+space +spat+space+ "[Pixels pr. m]"
)
toprow.pack()
midrow = tk.Frame(window)
globeview = tk.Label(midrow, image = img1)
globeview.pack(side = "left") # the side argument sets this to pack in a row rather than a column
mercatorview = tk.Label(midrow, image = img2)
mercatorview.pack(side = "left")
midrow.pack() # pack the toprow frame into the window
bottomrow = tk.Frame(window)
currentweather= tk.Label(bottomrow, image = img3)
currentweather.pack(side = "left")
gearth = tk.Label(bottomrow, image = img4)
gearth.pack(side = "left")
bottomrow.pack()
#Start the GUI
window.mainloop()
每個tkinter應用程序只需要一個Tk
類實例。 在您的代碼中,您不會創建一個,但
仍在創建的
mainloop
似乎會自動創建一個
(請參閱下面的Bryan的注釋),即使您以后不能(輕松地)引用它也是如此。 mainloop
如果您將使用當前Toplevel
插件之外的其他Toplevel
插件,請執行以下操作:
root = tk.Tk()
root.withdraw() # You can go root.iconify(), root.deiconify() later if you
# want to make this window visible again at some point.
# MAIN CODE HERE
root.mainloop()
如果不是簡單地替換:
window = tk.Toplevel()
與:
window = tk.Tk()
注意:還請注意,如果您使用的是IDLE,請記住它會創建自己的Tk
對象,這可能掩蓋了您的應用程序在獨立使用時需要一個Tk
對象的事實。
從window = tk.Toplevel()
刪除Toplevel
。 我沒有可用的python2 dist-我在python3上,但是當我從代碼中刪除TopLevel
時,它僅顯示一個窗口。 所以,python3的方式是...
import tkinter as tk
#This creates the main window of an application
window = tk.Tk()
#Start the GUI
window.mainloop()
我認為唯一的區別是python2的tkinter實際上是Tkinter(正如您已經做過的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.