Python TkInter按钮编程

时间:2017-02-10 03:59:49

标签: python button tkinter tk

我希望有人告诉我以下代码有什么问题。我希望代码提供一个带有2个按钮的窗口,一个说50%,另一个说75%。

import tkinter as tk

class Application(tk.Frame):
    def _init_(self, master=None):
        super ()._init_(master)
        self.pack()
        self.create_widget1()
        self.create_widget2()

    def create_widget1(self):
        self.test = tk.Button(self)
        self.test["text"] = "50%"
        self.test["command"] = self.choice1
        self.test.pack(side="top")
    def create_widget2(self):
        self.test = tk.Button(self)
        self.test["text"] = "75%"
        self.test["command"] =self.choice2
        self.test.pack(side="bottom")

        self.quit = tk.Button(self, text="QUIT", fg="black", command=root.destroy)
        self.quit.pack(side="bottom")

    def  choice1(self):
        print ("You have chosen a discount of 50%")
    def choice2(self):
        print ("You have chose a discount of 75%")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

1 个答案:

答案 0 :(得分:0)

您忘记在_周围使用双init。取代

def _init_(self, master=None):
        super ()._init_(master)

def __init__(self, master=None):
        super ().__init__(master)