簡體   English   中英

使用Python GUI創建按鈕

[英]Creating buttons with Python GUI

我正在嘗試使用類在Python中創建Buttons,但是在運行它時按鈕不會出現。 以下是我的代碼

#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()

任何幫助,將不勝感激

您需要將“構造函數”方法命名為__init__ ,而不是_init_ 在編寫時,因為從未調用_init_永遠不會調用gridcreate_widgets方法。

好的,第一個問題是您已經聲明了以下代碼:

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()

在此處輸入圖片說明

暫無
暫無

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

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