我正在制作一个带有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_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()
答案 0 :(得分:0)
这里定义了两个创建的按钮:
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()
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()
第二种方式:
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()
我为任何语言错误道歉,但英语不是我的母语。