簡體   English   中英

難以理解python中的Tkinter mainloop()

[英]Difficulty understand Tkinter mainloop() in python

好的,所以我有一個帶有EDIT和VIEW按鈕的基本窗口。 就我的代碼而言,EDIT和VIEW均返回一條消息“此按鈕無用”。 我在“ main_window”類下創建了這些文件。 我創建了另一個類“ edit_window”,希望在單擊“編輯”按鈕時調用它。 本質上,單擊“編輯”按鈕應更改為顯示帶有“添加”和“刪除”按鈕的新窗口。 到目前為止,這是我的代碼...下一個合理的步驟是什么?

from Tkinter import *
#import the Tkinter module and it's methods
#create a class for our program

class main_window:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack(padx=15,pady=100)

        self.edit = Button(frame, text="EDIT", command=self.edit)
        self.edit.pack(side=LEFT, padx=10, pady=10)

        self.view = Button(frame, text="VIEW", command=self.view)
        self.view.pack(side=RIGHT, padx=10, pady=10)

    def edit(self):
        print "this button is useless"

    def view(self):
        print "this button is useless"

class edit_window:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack(padx=15, pady=100)

        self.add = Button(frame, text="ADD", command=self.add)
        self.add.pack()

        self.remove = Button(frame, text="REMOVE", command=self.remove)
        self.remove.pack()

    def add(self):
        print "this button is useless"

    def remove(self):
        print "this button is useless"


top = Tk()
top.geometry("500x500")
top.title('The Movie Machine')
#Code that defines the widgets

main = main_window(top)


#Then enter the main loop
top.mainloop()

只需創建一個Toplevel而不使用Frame

class MainWindow:
    #...
    def edit(self):
        EditWindow()

class EditWindow(Toplevel):
    def __init__(self):
        Toplevel.__init__(self)
        self.add = Button(self, text="ADD", command=self.add)
        self.remove = Button(self, text="REMOVE", command=self.remove)
        self.add.pack()
        self.remove.pack()

我已經根據CapWords約定更改了類名(請參閱PEP 8 )。 這不是強制性的,但我建議您在所有Python項目中使用它以保持統一的樣式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM