2017-12-13 2 views
0

GUI를 생성하는 코드를 작성했습니다. 이제 코드를 실행할 때마다 기본 GUI 창과 아무것도없는 작은 창이 추가로 생성됩니다. 작은 창을 닫으면 큰 주 창이 사라집니다. 지금 비슷한 문제가있는 다른 게시물을 읽었지만 내 코드에서 오류의 위치를 ​​확인할 수 없었습니다.Extra Tkinter 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

window = tk.Toplevel()에서 Toplevel를 제거합니다. 파이썬 2 배포판을 사용할 수 없습니다. 파이썬 3을 사용하고 있지만 코드에서 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 인 것 같아요.

+0

제거하면 모듈을 호출 할 수 없다는 오류가 표시됩니다. 그래, 주로 3을 사용하지만이 작업에서 python2를 사용해야합니다. py2 imo에서 tkinter를 사용하는 엉덩이에 큰 고통이 있습니다. – Kongstad

+0

네, 가져온 방법 때문입니다. 'tkinter를 tk로 가져 오기'하고 'window = tk.Tk()'하면 어떻게됩니까? –

+0

또한 필자는 필자의 대답을 편집하여 필자의 가져 오기가 귀하의 것을 더 잘 반영하도록하였습니다 (대문자 사용 제외). 희망이 도움이됩니다! :) –

6

모든 tkinter 응용 프로그램에는 정확히 Tk 클래스의 인스턴스가 필요합니다. 코드에서 하나를 만들지는 않지만 mainloop은 (나중에) (쉽게) 참조 할 수는 없지만 여전히 자동으로 생성됩니다 ( Bryan 님의 댓글 아래에 있음).

당신이 당신의 curent 한 가지의 추가적인 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을 사용하여 작업 할 경우 자체적으로을 생성한다는 점에 유의하십시오. 독립 실행 형 응용 프로그램을 사용할 때 응용 프로그램이 필요하다는 사실을 숨길 수있는개체.

+2

루트 창을 만드는 것은'mainloop '이 아닙니다. 처음으로 위젯을 만들면 루트 창이 생성됩니다 (존재하지 않는 경우). –

+1

답변 해 주셔서 감사합니다. 나는 그가 처음이었던 것처럼 다른 하나를 적립했다. 그러나 너는 모두 같은 결론에 도달했다. 고맙습니다 =) – Kongstad