如何在Python中制作更多tkinter按钮
我正在为一个学校项目制作一个带有gui的PC助手程序。我已经添加了两个按钮,但我似乎不能添加超过两个 代码如下:如何在Python中制作更多tkinter按钮,python,tkinter,python-3.6,Python,Tkinter,Python 3.6,我正在为一个学校项目制作一个带有gui的PC助手程序。我已经添加了两个按钮,但我似乎不能添加超过两个 代码如下: #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_w
#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()
此处定义了两个创建的按钮:
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")
但为什么只有这些被创造出来?
答案很简单,请查看下面代码中的注释:
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")
此代码给出的效果与下面的代码相同
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")
定义函数类似于分配变量值:
a = 1
a = 4
a = 150
print(a) # will print 150, not 1, 4, 150
因此,您应该将所有按钮(将来还有其他小部件)放在同一个函数中,或者更改函数名并在init函数中调用它们
工作代码(第一种方式,我认为更好):
第二种方式:
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()
对于任何语言错误,我深表歉意,但英语不是我的母语。如果您尝试添加两个以上的按钮,会发生什么情况?@halfer如果您碰巧注意到它丢失了,请在编辑标记为[python-y.x]的问题时添加通用python标记。谢谢@安德拉斯:会的,是的-会试着发现它。
:)
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()