简体   繁体   English

如何在Python中制作更多的Tkinter按钮

[英]How to make more tkinter buttons in Python

I am making a PC helper program with a gui for a school project. 我正在为一个学校项目的gui开发一个PC帮助程序。 I have added two buttons but I cant seem to add more than two... 我添加了两个按钮,但是我似乎无法添加两个以上...

Here is the code: 这是代码:

#All imports go here
import tkinter as tk
import os

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello, what would you like help with today?"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

    def create_widgets(self):
        self.notepad = tk.Button(self)
        self.notepad["text"] = "Launch Notepad"
        self.notepad["command"] = self.notepad
        self.notepad.pack(side="right")

    def create_widgets(self):
        self.hi = tk.Button(self)
        self.hi["text"] = "Launch CMD"
        self.hi["command"] = self.cmd
        self.hi.pack(side="left") 

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

    def cmd(self):
        os.system("start cmd /a")

    def notepad(self):
        os.system("start notepad")

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

sys.exit()

Two created buttons are definded here: 在此定义了两个创建的按钮:

def create_widgets(self):
    self.hi = tk.Button(self)
    self.hi["text"] = "Launch CMD"
    self.hi["command"] = self.cmd
    self.hi.pack(side="left")

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

But why only these are created? 但是为什么只创建这些? Answer is simple, look comments in code below: 答案很简单,请在下面的代码中查看注释:

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()
        self.create_widgets2()
        self.create_widgets3()

    def create_widgets(self):  # define
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello, what would you like help with today?"
        self.hi_there["command"] = self.say_hi  # additional bug (AttributeError: 'Application' object has no attribute 'say_hi'), change this to "self.hi_there["command"] = self.hi_there"
        self.hi_there.pack(side="top")

    def create_widgets(self):  # re-define without usage
        self.notepad = tk.Button(self)
        self.notepad["text"] = "Launch Notepad"
        self.notepad["command"] = self.notepad
        self.notepad.pack(side="right")

    def create_widgets(self):  # again re-define without usage
        self.hi = tk.Button(self)
        self.hi["text"] = "Launch CMD"
        self.hi["command"] = self.cmd
        self.hi.pack(side="left")

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

    def cmd(self):
        os.system("start cmd /a")

    def notepad(self):
        os.system("start notepad")

This code gives the same efect as code below. 此代码与下面的代码具有相同的效果。

class Application(tk.Frame):
def __init__(self, master=None):
    super().__init__(master)
    self.pack()
    self.create_widgets()

def create_widgets(self):
    self.hi = tk.Button(self)
    self.hi["text"] = "Launch CMD"
    self.hi["command"] = self.cmd
    self.hi.pack(side="left")

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

def cmd(self):
    os.system("start cmd /a")

def notepad(self):
    os.system("start notepad")

Defining functions is similar to assigning the variable value: 定义函数类似于分配变量值:

a = 1
a = 4
a = 150
print(a)  # will print 150, not 1, 4, 150

So you should put all the buttons (other widgets in future too) in the same function or change the function names and call them in the init function. 因此,您应该将所有按钮(将来也包括其他小部件)放在同一函数中,或者更改函数名称并在init函数中调用它们。

Working code (first way, better in my opinion): 工作代码(第一种方法,我认为更好):

import tkinter as tk
import os


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello, what would you like help with today?"
        self.hi_there["command"] = self.hi_there
        self.hi_there.pack(side="top")

        self.notepad = tk.Button(self)
        self.notepad["text"] = "Launch Notepad"
        self.notepad["command"] = self.notepad
        self.notepad.pack(side="right")

        self.hi = tk.Button(self)
        self.hi["text"] = "Launch CMD"
        self.hi["command"] = self.cmd
        self.hi.pack(side="left")

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

    def cmd(self):
        os.system("start cmd /a")

    def notepad(self):
        os.system("start notepad")


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

Second way: 第二种方式:

import tkinter as tk
import os


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()
        self.create_widgets2()
        self.create_widgets3()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello, what would you like help with today?"
        self.hi_there["command"] = self.hi_there
        self.hi_there.pack(side="top")

    def create_widgets2(self):
        self.notepad = tk.Button(self)
        self.notepad["text"] = "Launch Notepad"
        self.notepad["command"] = self.notepad
        self.notepad.pack(side="right")

    def create_widgets3(self):
        self.hi = tk.Button(self)
        self.hi["text"] = "Launch CMD"
        self.hi["command"] = self.cmd
        self.hi.pack(side="left")

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

    def cmd(self):
        os.system("start cmd /a")

    def notepad(self):
        os.system("start notepad")


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

I apologize for any language mistakes, but English is not my native language. 对于任何语言错误,我深表歉意,但英语不是我的母语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM