【问题标题】:Python TkInter Buttons ProgrammingPython TkInter 按钮编程
【发布时间】:2017-02-10 03:59:49
【问题描述】:

我希望有人告诉我以下代码有什么问题。我希望代码提供一个带有 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()

【问题讨论】:

  • 我对 Tkinter 不太熟悉,但你在对 create_widget2() 的调用中覆盖了 self.test。
  • 没关系,因为这意味着我在我命名为 test 的主函数中操作

标签: python button tkinter tk


【解决方案1】:

您忘记在 init 周围使用双精度 _。替换

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

与

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2018-11-21
    • 2017-07-23
    • 2020-06-19
    • 2013-04-11
    相关资源
    最近更新 更多