【问题标题】:Creating buttons with Python GUI使用 Python GUI 创建按钮
【发布时间】:2012-10-15 17:22:19
【问题描述】:

我正在尝试使用类在 Python 中创建按钮,但是在运行它时按钮不会出现。以下是我的代码

#Button_2
#Using Classes

from Tkinter import * 

class Application(Frame):
    """A GUI application with three button"""

    def _init_(self, master):
        """ Initialise the Frame. """
        Frame._init_(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        #"""Create three buttons"""
        #Create first buttom
        self.btn1 = Button(self, text = "I do nothing")
        self.btn1.grid()

        #Create second button
        self.btn2 = Button(self)
        self.btn2.grid()
        self.btn2.configure(text = "T do nothing as well")

        #Create third button
        self.btn3 = Button(self)
        self.btn3.grid()    
        self.btn3.configure(text = "I do nothing as well as well")

    root = Tk()
    root.title("Lazy Button 2")
    root.geometry("500x500")
    app = Application(root)

    root.mainloop()

任何帮助将不胜感激

【问题讨论】:

  • 添加到 mgilson 的答案中,您的缩进不存在。
  • 我为您缩进了您的代码,以便它在 SO 上正确呈现。渲染的部分问题是您似乎有制表符和空格。 (我将它们全部转换为 PEP8 中指定的每级 4 个空格)。希望这只是为了在 SO 上发布代码,但如果不是,请不要在代码中混合制表符和空格,最好像我上面所做的那样,每个缩进级别使用 4 个空格。跨度>

标签: python class user-interface button tkinter


【解决方案1】:

您需要将“构造函数”方法命名为__init__,而不是_init_。正如它所写的那样,您的 gridcreate_widgets 方法永远不会被调用,因为 _init_ 永远不会被调用。

【讨论】:

    【解决方案2】:

    好的,第一个问题是您已经声明了以下代码:

    root = Tk()
    root.title("Lazy Button 2")
    root.geometry("500x500")
    app = Application(root)
    
    root.mainloop()code here
    

    在类本身内部。它应该在外面,所以这是一个缩进问题(可能是缩进的stackoverflow问题?)。

    其次,我简化了代码以使其运行

    from Tkinter import * 
    
    class Application(Frame):
          """A GUI application with three button"""
    
         #create a class variable from the root (master):called by the constructor
         def _init_(self, master):
              self.master = master
    
         #simple button construction
         # create a button with chosen arguments
         # pack it after the creation not in the middle or before
    
         def create_widgets(self):
              #"""Create three buttons"""
              #Create first button
              btn1 = Button(self.master, text = "I do nothing")
              btn1.pack()
    
              #Create second button
              btn2 = Button(self.master, text = "T do nothing as well")
              btn2.pack()
    
             #Create third button
             btn3=Button(self.master, text = "I do nothing as well as well")
             btn3.pack()
    
      #must be outside class definition but probably due to stackoverlow
      root = Tk()
      root.title("Lazy Button 2")
      root.geometry("500x500")
      app = Application(root)
      #call the method
      app.create_widgets()
      root.mainloop()
    

    这是一个起点,肯定会如下所示:

    您可能可以使用 grid() 而不是 pack 并从 def init 构造函数中调用该方法。希望对您有所帮助。

    这个调用方法也可以:

    root = Tk()
    root.title("Lazy Button 2")
    root.geometry("500x500")
    app = Application(root).create_widgets()  #creates and invokes
    root.mainloop()
    

    我的最后一次尝试也有效:

    def __init__(self,master):
        self.master = master
        self.create_widgets()
    

    接着是:

    root = Tk()
    root.title("Lazy Button 2")
    root.geometry("500x500")
    app = Application(root)
    root.mainloop()
    

    最终代码:

    from Tkinter import * 
    
    class Application(Frame):
    """A GUI application with three button"""
    
    def __init__(self,master):
        self.master = master
        self.create_widgets()
    
    
    
    def create_widgets(self):
        #"""Create three buttons"""
        #Create first buttom
        btn1 = Button(self.master, text = "I do nothing")
        btn1.pack()
    
        #Create second button
        btn2 = Button(self.master, text = "T do nothing as well")
        btn2.pack()
    
        #Create third button
        btn3=Button(self.master, text = "I do nothing as well as well")
        btn3.pack()
    
    root = Tk()
    root.title("Lazy Button 2")
    root.geometry("500x500")
    app = Application(root)
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多