【问题标题】:How would make it so I can apply a button to any window I want?如何做到这一点,以便我可以将按钮应用于我想要的任何窗口?
【发布时间】:2021-08-07 02:46:24
【问题描述】:

我在措辞上遇到了问题,所以让我解释一下。我正在学习使用面向对象编程作为项目的一部分,所以我决定从创建用于创建窗口的类和放置在窗口上的按钮开始(标签等。稍后也会出现)。换句话说,我希望能够将按钮放在“Window1”上,而无需在类中指定“Window1”(而是使用 CreateButton(values) 传递它。到目前为止,这是我的代码,并得到了帮助。 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")

【问题讨论】:

  • 你为什么不从tk.Button继承?还是tk.Frame
  • 顺便说一句,如果不是从任何东西继承,那些()括号是不必要的,只需使用class ClassName:,如果你要继承然后使用class ClassName(InheritFrom):并且不要忘记在@ 987654327@ 方法使用super().__init__()InheritFrom.__init__(self)(但也许与self 相反)
  • 当您在 Tkinter 中创建任何小部件时,您必须将 parent 设置为第一个参数 - 这样它就知道在哪里显示它。你应该对你的类做同样的事情 - win = CreateWindow(...)CreateButton(win, ...) 但是你应该继承类 CreateWindow(tk.Tk)CreateButton(tk.Button) 来获得它。或者您应该创建使用return name 的函数create_window()create_button(),因为您的代码更适合函数。并且带有单词create 的名称也很适合函数。

标签: python class oop tkinter


【解决方案1】:

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

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


顺便说一句:

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()

见:PEP 8 -- Style Guide for Python Code -

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 2020-12-17
    相关资源
    最近更新 更多