额外的Tkinter GUI弹出窗口

时间:2017-12-13 18:21:09

标签: python user-interface tkinter

我写了一堆产生GUI的代码。现在每当我运行代码时,它都会生成主GUI窗口和一个没有任何内容的小窗口。当我关闭较小的窗口时,大的主窗口消失。 现在我一直在阅读其他类似问题的帖子,但我无法确定代码中的错误位置。

请帮助:)

跟进问题:如何添加背景图像而不是灰色无聊颜色?

这是它的样子。 enter image description here

#%% 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()

2 个答案:

答案 0 :(得分:7)

每个tkinter应用程序只需要Tk类的一个实例。在你的代码中你不创建一个但是 mainloop似乎自动创建一个它仍然被创建(参见下面的Bryan评论),即使你不能(很容易)稍后再提及。

如果你要使用额外的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对象,这可能会隐藏您的应用程序在独立使用时需要的对象。

答案 1 :(得分:2)

Toplevel移除window = tk.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(正如你已经做过的那样)。