[英]Why my tkinter back button does not function?
我在我的 tkinter 中做了 2 頁。 主頁有一個按鈕,可以將用戶帶到 Classifier_UI 頁面。 在 Classifier_UI 頁面中,我制作了一個后退按鈕,將用戶帶回主頁面。 如果我只運行 Classifier_UI.py,后退按鈕就可以正常工作。 它確實讓我回到了主頁。 但是,如果我從 main_page.py 運行,然后單擊將我帶到 Classifier_UI 頁面的按鈕,然后單擊后退按鈕,窗口將自動關閉並且沒有將我帶回主頁面。 為什么會發生這種情況,我該如何解決?
下面是我在主頁上制作的按鈕代碼:
from tkinter import *
class Test:
def __init__(self, tk):
fm = Frame(tk)
self.l1 = Label(tk, text="Welcome", font=("Helvetica", 38, 'bold'), bg='LightGoldenrod2', fg='gray24').place(relx=0.25, rely=0.15)
self.b2 = Button(tk, bg='LightGoldenrod4', text="Classifier 1", font=("Helvetica", '10', 'bold'), foreground="white", width = 24, height = 3, command=self.change).pack(side=LEFT, expand=YES, padx=110, pady=300)
fm.pack(fill=BOTH, expand=YES)
def change(self):
tk.destroy()
import Classifier_UI
img_path = StringVar()
canvas = tk.Canvas(root, height = '700', width= '750', bg='#292828')
canvas.pack(fill=BOTH)
tk = Tk()
tk['bg']='LightGoldenrod2'
tk.toolbar = Frame(tk, bg="white")
width= tk.winfo_screenwidth()
height= tk.winfo_screenheight()
tk.geometry("%dx%d" % (width, height))
tt = Test(tk)
tk.mainloop()
這是我第二頁中按鈕的代碼:
def change():
root.destroy()
import main_page
root = tk.Tk()
root.title("Image Classifier")
browse = tk.Button(text="Back", bg='white', font='courier 10', command=change)
browse.place(relx = 0.75 ,rely = 0.85, relwidth = 0.12, relheight=0.05)
root.mainloop()
問題是tk.destroy()
和root.destroy()
關閉了窗口,因為您調用的是窗口的 destroy() 方法,而不是第一幀。 嘗試在您的類引用框架時執行self.destroy()
,這只會破壞框架而不是整個窗口,但是,當框架被破壞時,除了創建另一個框架外,您別無他法實例。 此外,您不應該在函數內部導入模塊,除非您想讓該導入成為可選,否則將所有導入放在腳本的開頭。
更重要的是,在這兩個文件中,您都創建了一個根窗口並將其銷毀,如果您只想更改顯示的框架,則沒有必要這樣做。 我建議你在窗口內放兩個框架,一個在另一個上面,按下按鈕時在框架之間來回切換。
這是一個簡單的代碼示例,在兩個框架之間進行按鈕切換(請注意,我將按鈕直接放在窗口內而不是放在框架內以使其獨立於更改,但是如果您願意,您可以為每個框架放置一個想):
import tkinter as tk
class MainWindow(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frame_on_top = 1
self.switch_frame_button = tk.Button(self, text='switch', command=self.switch_frame)
self.switch_frame_button.grid(row=0, column=0)
self.container = tk.Frame(self) # This is the position where we will put the frames.
self.container.grid(row=1, column=0)
self.frames = {}
counter = 2
# Now let's make our frames.
# Starting from the second, so that the first frame will be put on top (last in first out).
for fr in (SecondFrame, FirstFrame):
frame = fr(parent=self)
self.frames[str(counter)] = frame
frame.grid(row=1, column=0, sticky='nsew') # Put all the frames in the same spot.
counter -= 1
def switch_frame(self):
if self.frame_on_top == 1:
frame = self.frames['2']
frame.tkraise()
self.frame_on_top = 2
elif self.frame_on_top == 2:
frame = self.frames['1']
frame.tkraise()
self.frame_on_top = 1
class FirstFrame(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
tk.Label(self, text='First frame').pack()
class SecondFrame(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
tk.Label(self, text='Second frame').pack()
root = MainWindow()
root.mainloop()
嘗試制作一個按鈕,它的命令返回到該功能
def BackFunction(self):
pass
def __init__(self):
back = Button(root, command=self.BackFunction)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.