自定义 GUI 元素的类未显示

Posted

技术标签:

【中文标题】自定义 GUI 元素的类未显示【英文标题】:Class for custom GUI element not showing 【发布时间】:2018-04-15 20:20:07 【问题描述】:

我正在尝试创建一个如下图所示的 GUI:

以下是代码:

#! /usr/bin/python3

import tkinter as tk

# CLASS FOR OWN PANEL: 
class Mypanel(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.label = tk.Label(self, text="Enter here:")
        self.entry = tk.Entry(self)
        self.label.pack()
        self.entry.pack()

# FN FOR BUTTON:
def adder():
    main_label.set((panel1.entry.get() + panel2.entry.get()))

# CREATE GUI ELEMENTS: 
main_window = tk.Tk()       # CREATE MAIN WINDOW
panel1 = Mypanel()          # CREATE A PANEL
panel2 = Mypanel()          # CREATE ANOTHER PANEL
main_label = tk.Label(main_window, text="Press button to get values.")  
main_button = tk.Button(main_window, text="Get values.", command=adder) 

# PACK ALL GUI ELEMENTS IN main WINDOW
panel1.pack()
panel2.pack()
main_label.pack()
main_button.pack()

# SHOW MAIN WINDOW: 
main_window.mainloop()

但是,它给出了以下错误:

$ ./guipanels.py
Traceback (most recent call last):
  File "./guipanels.py", line 29, in <module>
    panel1.pack()
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1961, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'pack'

问题出在哪里,如何解决?感谢您的帮助。

【问题讨论】:

任何理由选择 Frame 还是 PanedWindow,因为我有 2 个非常好的答案。 【参考方案1】:

您需要的不是Tk 类,而是Frame

import tkinter as tk

# CLASS FOR OWN PANEL:
class Mypanel(tk.Frame):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, kwargs)
        self.label = tk.Label(self, text="Enter here:")
        self.entry = tk.Entry(self)

        self.label.pack()
        self.entry.pack()
        # def on_button(self):
            # print(self.entry.get())

# FN FOR BUTTON:
def adder():
    main_label.set((panel1.entry.get() + panel2.entry.get()))

# CREATE GUI ELEMENTS:
main_window = tk.Tk()       # CREATE MAIN WINDOW
panel1 = Mypanel(main_window)          # CREATE A PANEL
panel2 = Mypanel(main_window)          # CREATE ANOTHER PANEL
main_label = tk.Label(main_window, text="Press button to get values.")
main_button = tk.Button(main_window, text="Get values.", command=adder)

# PACK ALL GUI ELEMENTS IN main WINDOW
panel1.pack()
panel2.pack()
main_label.pack()
main_button.pack()

# SHOW MAIN WINDOW:
main_window.mainloop()

您可以将Tk 类视为主应用程序,并将小部件放入其中。一个代码中应该只有一个Tk

【讨论】:

【参考方案2】:

您的意思是继承tk.PanedWindow 吗?

试试这个:

class Mypanel(tk.PanedWindow):
    def __init__(self, mainwindow):
        super(Mypanel, self).__init__(mainwindow)
        self.label = tk.Label(self, text="Enter here:")
        self.entry = tk.Entry(self)

        self.label.pack()
        self.entry.pack()
        # def on_button(self):
            # print(self.entry.get())

并将代码中的main_window 传递给面板:

main_window = tk.Tk()       # CREATE MAIN WINDOW
panel1 = Mypanel(main_window)          # CREATE A PANEL
panel2 = Mypanel(main_window)          # CREATE ANOTHER PANEL

【讨论】:

以上是关于自定义 GUI 元素的类未显示的主要内容,如果未能解决你的问题,请参考以下文章

自定义类未出现在情节提要中

故事板自定义类未实例化 UIView 类。为啥?目标 C / Xcode

Fxcop 自定义规则未显示在 Fxcop GUI 中

如何在 NetBeans GUI Builder 中包含自定义面板?

使用 Qt GUI 编写 c++ dll,在自定义小部件中显示失败

自定义控件,winapi