如何才能将按钮应用到我想要的任何窗口



我在措辞上有困难,所以让我解释一下。我正在学习将面向对象编程作为项目的一部分,所以我决定从创建用于创建窗口的类和放置在窗口上的按钮开始(标签等稍后也会出现(。换句话说,我希望能够将按钮放置在";窗口1";而没有指定";窗口1";在类中(并使用CreateButton(值(传递它(。到目前为止,这是我的代码,并得到了帮助。PS。在我尝试windowname参数之前,它的功能是应该的,我手动将窗口设置为home_window,但我似乎无法正确通过windowname参数。

import tkinter as tk
from tkinter import ttk
home_window = tk.Tk()
home_window.title("Rota System")
home_window.geometry("600x600")
def thankyou():
print ("Thank you very much")
class CreateWindow():
def __init__(self, name, title, geometry):
self.name = name
self.title = title
self.geometry = geometry
name = tk.Tk()
name.title(title)
name.geometry(geometry)

class CreateButton():
def __init__(self, name, width, height, x, y, font, size, text, command, windowname):
#I want to be able to place the button on any window I want, not just home_window
self.name = name
self.width = width
self.height = height
self.x = x
self.y = y
self.font = font
self.size = size
self.text = text
self.command = command
self.windowname = windowname
name = tk.Button(windowname, text=text, command=command)
name.config(font=(font, size))
name.place(x=x, y=y, height=height, width=width)
CreateWindow("Test", "Test", "900x600")
CreateButton("Test","200","200","0","0","Courier",10,"Test",thankyou, "Test")

首先:tkinter需要由Tk()Button()等返回的对象ID,而不是您创建的名称(字符串(。

对于字符串名称,您必须保留全局字典{name: object_id}才能将name转换为object_id


BTW:

Tk()应仅用于创建主窗口。对于其他窗口,您应该使用Toplevel()


类中的代码(以及它们的名称(非常适合函数。

函数创建对象并使用return返回此对象,以便在其他函数中使用。

import tkinter as tk
from tkinter import ttk
# --- functions ---
def thank_you():
print ("Thank you very much")
def create_window(title, geometry):
window = tk.Tk()
window.title(title)
window.geometry(geometry)

return window
def create_button(parent, width, height, x, y, font, size, text, command):
button = tk.Button(parent, text=text, command=command)
button.config(font=(font, size))
button.place(x=x, y=y, height=height, width=width)
return button
# --- main ---
window = create_window("Test", "900x600")
button = create_button(window, 200, 200, 0, 0, "Courier", 10, "Test", thank_you)
window.mainloop()

使用类我会继承它。所以我可以将实例附加到变量中,并在其他类中用作父类。

我会使用nouns作为类的名称。

import tkinter as tk
from tkinter import ttk
# --- classes ---
class MainWindow(tk.Tk):

def __init__(self, name, title, geometry):
super().__init__()

self.name = name
self.title(title)
self.geometry(geometry)
#self._title = title
#seff._geometry = geometry

class MyButton(tk.Button):

def __init__(self, parent, name, width, height, x, y, font, size, text, command):
super().__init__(parent, text=text, command=command)

self.name = name
self.config(font=(font, size))
self.place(x=x, y=y, height=height, width=width)

#self._width = width
#self._height = height
#self._x = x
#self._y = y
#self._font = font
#self._size = size
#self._text = text
#self._command = command

# --- functions ---
def thank_you():
print("Thank you very much")
# --- main ---
window = MainWindow("WindowName", "Test", "900x600")
button = MyButton(window, "ButtonName", 200, 200, 0, 0, "Courier", 10, "Test", thank_you)
window.mainloop()

请参阅:PEP8——Python代码样式指南-

我不知道是否有关于类名的nouns和函数的verbs(或者可能在《Clean Code, Robert C. Martin》一书中(,但通常人们会使用这个规则。

相关内容

最新更新