Python - Tkinter:Window class
Python - Tkinter: Window class
如何使用 class 在 tkinter 中创建 window?
我知道 root = Tk()
是标准的方法,但是如果我想制作一个 class 在 python 中创建一个 "window" 我可以添加稍后的按钮是谁完成的?
我从下面的代码中得到这个错误:
Traceback (most recent call last):
File "C:/Users/euc/PycharmProjects/Skole/Shortcut/Gui2.py", line 26, in <module>
b = Create_button(a)
File "C:/Users/euc/PycharmProjects/Skole/Shortcut/Gui2.py", line 18, in __init__
self.button = Button(window, text=text)
File "C:\Python34\lib\tkinter\__init__.py", line 2161, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 2084, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python34\lib\tkinter\__init__.py", line 2062, in _setup
self.tk = master.tk
AttributeError: 'Create_window' object has no attribute 'tk'
我的代码:
from tkinter import *
class Create_window(): #Create a window
win = Tk()
def __init__(self):
self = Tk()
self.title("This is title Name")
class Create_button: # Create a button within the window
text = "None"
button_width = 15
button_height = 10
def __init__(self, window, text="null"):
if text == "null": #Set text to none if none parameter is passed
text = self.text
self.button = Button(window, text=text)
self.button.grid(row=0, column=0)
a = Create_window() # Create window
b = Create_button(a) # Create button within Window
a.mainloop()
您可以子class Toplevel
来执行此操作。查看 this 以了解有关 Toplevel
的更多信息。
我还稍微清理并重组了您的代码。将您的代码与此进行比较,并注意为获得更好的实践所做的差异和更改。
注意 class 名称约定而不是方法名称约定。 Class 名称通常不以动词或动作词开头,因为本质上 classes 是宾语或名词。另一方面,函数和方法必须以动词或动作词开头。
您不需要使用 class 为看起来可以放在单个函数中的东西创建按钮。
这是修改后的代码段:
from tkinter import *
class MyWindow(Toplevel): #Create a window
def __init__(self, master=None):
Toplevel.__init__(self, master)
self.title("This is title Name")
def create_button(frame, text="None"): # Create a button within the frame given
button_width = 15
button_height = 10
frame.button = Button(frame, text=text)
frame.button. configure(height=button_height, width=button_width)
frame.button.grid(row=0, column=0)
app = Tk()
a = MyWindow(master=app) # Create window
create_button(a) # Create button within Window
app.mainloop()
还请确保检查此内容以了解 great tutorial 使用和子classing Toplevel 来创建对话框。
希望对您有所帮助。
如何使用 class 在 tkinter 中创建 window?
我知道 root = Tk()
是标准的方法,但是如果我想制作一个 class 在 python 中创建一个 "window" 我可以添加稍后的按钮是谁完成的?
我从下面的代码中得到这个错误:
Traceback (most recent call last):
File "C:/Users/euc/PycharmProjects/Skole/Shortcut/Gui2.py", line 26, in <module>
b = Create_button(a)
File "C:/Users/euc/PycharmProjects/Skole/Shortcut/Gui2.py", line 18, in __init__
self.button = Button(window, text=text)
File "C:\Python34\lib\tkinter\__init__.py", line 2161, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 2084, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python34\lib\tkinter\__init__.py", line 2062, in _setup
self.tk = master.tk
AttributeError: 'Create_window' object has no attribute 'tk'
我的代码:
from tkinter import *
class Create_window(): #Create a window
win = Tk()
def __init__(self):
self = Tk()
self.title("This is title Name")
class Create_button: # Create a button within the window
text = "None"
button_width = 15
button_height = 10
def __init__(self, window, text="null"):
if text == "null": #Set text to none if none parameter is passed
text = self.text
self.button = Button(window, text=text)
self.button.grid(row=0, column=0)
a = Create_window() # Create window
b = Create_button(a) # Create button within Window
a.mainloop()
您可以子class Toplevel
来执行此操作。查看 this 以了解有关 Toplevel
的更多信息。
我还稍微清理并重组了您的代码。将您的代码与此进行比较,并注意为获得更好的实践所做的差异和更改。
注意 class 名称约定而不是方法名称约定。 Class 名称通常不以动词或动作词开头,因为本质上 classes 是宾语或名词。另一方面,函数和方法必须以动词或动作词开头。
您不需要使用 class 为看起来可以放在单个函数中的东西创建按钮。
这是修改后的代码段:
from tkinter import *
class MyWindow(Toplevel): #Create a window
def __init__(self, master=None):
Toplevel.__init__(self, master)
self.title("This is title Name")
def create_button(frame, text="None"): # Create a button within the frame given
button_width = 15
button_height = 10
frame.button = Button(frame, text=text)
frame.button. configure(height=button_height, width=button_width)
frame.button.grid(row=0, column=0)
app = Tk()
a = MyWindow(master=app) # Create window
create_button(a) # Create button within Window
app.mainloop()
还请确保检查此内容以了解 great tutorial 使用和子classing Toplevel 来创建对话框。
希望对您有所帮助。