我的Tkinter窗口大部分時間顯示正確,但隨後有時會隨機顯示。這是一個主要的痛苦。我已經嘗試了各種各樣的東西(例如在Tkinter窗口構造期間忽略了任何Pygame渲染),並且我已經確認窗口座標在顯示或不顯示時完全相同。我試圖lift
的窗口,我試圖在窗口上force_focus
,但沒有任何工作。爲什麼我的Tkinter窗口有時不顯示?
這裏是我的代碼:
# Function to take a postcode and station using Tkinter:
def first_time_setup():
global test_w, test_r, default_colour
master = Tk()
master.withdraw()
master.overrideredirect(1)
back_img = PhotoImage(file=find_data_file('Outlooker_background.png'))
back_label = Label(master, image=back_img)
back_label.place(x=0, y=0, relwidth=1, relheight=1)
back_label.image = back_img
text_font = font.Font(family='Arial', size=12)
button_font = font.Font(family='Calibri Light', size=11, weight="bold")
master.columnconfigure(0, weight=3)
e_weather = Entry(master, font=text_font, width=15)
e_weather.grid(row=0, column=1, padx=5, pady=(15, 5), sticky=E)
b_weather = Button(master, text="TEST", font=button_font,
bg='light blue', command=lambda: test_weather(e_weather.get(), b_weather, b_done))
b_weather.grid(row=0, column=2, padx=(0, 15), pady=(12, 0), sticky=E)
e_rail = Entry(master, font=text_font, width=15)
e_rail.grid(row=1, column=1, padx=5, pady=5, sticky=E)
b_rail = Button(master, text="TEST", font=button_font,
bg='light blue', command=lambda: test_rail(e_rail.get(), b_rail, b_done))
b_rail.grid(row=1, column=2, padx=(0, 15), sticky=E)
b_done = Button(master, text="DONE", font=button_font, state=DISABLED,
bg='light blue', command=lambda: testing_done(master))
b_done.grid(row=2, column=2, padx=(0, 15), pady=10, sticky=E)
if first_time:
b_quit = Button(master, text="QUIT", font=button_font,
bg='light blue', command=lambda: sys.exit(0))
else:
b_quit = Button(master, text="EXIT", font=button_font,
bg='light blue', command=lambda: master.destroy())
pygame.draw.rect(screen, black, (223, 296, 417, 134))
pygame.display.flip()
b_quit.grid(row=2, padx=15, pady=10, sticky=W)
default_colour = b_quit.cget("background")
master.update_idletasks() # Update "requested size" from geometry manager
x = (master.winfo_screenwidth() - 417)/2
y = (master.winfo_screenheight() - 134)/2
master.geometry("417x134+%d+%d" % (x, y))
master.deiconify()
master.lift()
master.focus_force()
e_weather.focus_set()
master.mainloop()
由於窗顯示約一半的時候,我認爲我的代碼要被罰款,這可能與兩種TKinter或者甚至是Windows的故障?有人知道發生了什麼事嗎?
編輯:這裏是調用first_time_setup
:
if first_time:
first_time_setup()
first_time = False
所以任何進一步的調用將觸發函數內的else
聲明。
如何調用'first_time_setup'? – SneakyTurtle
@SneakyTurtle:我編輯了問題以顯示呼叫。 – JasonPy