你好我想從頂層視窗呼叫根視窗中的一個函式。我想從首選項視窗(帶引數)的第一個視窗中創建新選項卡但我收到一個錯誤代碼:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\mircirc\gui_preferences.py", line 12, in add_tab
master.new_tab()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 2354, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'new_tab'
我忘記或不明白什么?
這是我的第一個根視窗:它創建了一些選項卡,我想使用根函式 new_tab() 從頂級視窗創建更多選項卡
from tkinter import ttk
from gui_preferences import *
class GUI_PRINCIPAL():
def __init__(self):
salons = ["#Radio-Evasion", "#un-autre-regard"]
root = Tk()
notebook = ttk.Notebook(root)
ttk.Frame(notebook)
root.title("Principale")
def ouvre_gui_preferences():
GUI_PREFERENCES(root)
def do_something():
print("Menu clicked")
def create_menu_bar(self):
menu_bar = Menu(self)
menu_file = Menu(menu_bar, tearoff=0)
menu_file.add_command(label="New", command=do_something)
menu_file.add_command(label="Save", command=do_something)
menu_file.add_separator()
menu_file.add_command(label="Preferences", command=ouvre_gui_preferences)
menu_file.add_separator()
menu_file.add_command(label="Exit", command=self.quit)
menu_bar.add_cascade(label="File", menu=menu_file)
menu_outils = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Outils", menu=menu_outils)
# menu_outils.add_command(label="Annonces", command=ouvre_annonces)
menu_outils.add_command(label="!cmd", command=ouvre_gui_preferences)
menu_help = Menu(menu_bar, tearoff=0)
menu_help.add_command(label="About", command=do_something)
menu_bar.add_cascade(label="Help", menu=menu_help)
self.config(menu=menu_bar)
def add_tab(tab_text):
tab_name = Frame(notebook)
notebook.add(tab_name, text="{}".format(tab_text))
def new_tab():
tab_name = Frame(notebook)
notebook.add(tab_name, text="{}".format("tab_text"))
# creation Menu
create_menu_bar(root)
# Création des Tabs salons
for i, salon in enumerate(salons):
add_tab(salons[i])
notebook.pack(expand=True, fill="both")
root.mainloop()
gui = GUI_PRINCIPAL()
這是我的 TopLevel 視窗
from tkinter import *
from tkinter.ttk import *
class GUI_PREFERENCES(Toplevel):
def __init__(self, master=None, irc=None):
super().__init__(master=master)
self.title("Preferences")
self.geometry(" 90 90")
def add_tab():
master.new_tab()
self.btn_add = Button(self, text="Append tab", state='normal', command=add_tab)
self.btn_add.grid(row=1, column=0, padx=(5, 0))v
uj5u.com熱心網友回復:
問題
- 目前,您的函式是一個本地函式,
new_tab()只有坐在__init__.GUI_PRINCIPAL GUI_PRINCIPAL頂層無法訪問位于其中的函式,因為您將GUI_PRINCIPAL.rootas master 傳遞給頂層。并且該tk.Tk物件沒有new_tab功能。
修復
- 將函式移出
__init__方法GUI_PRINCIPAL - 您可以使
GUI_PRINCIPAL類繼承自tk.Tk,然后這樣做會容易得多,或者您可以將GUI_PRINCIPAL用作頂級類中的主引數的引數。
遵循一些命名約定可能會使代碼看起來更干凈,但這留給你。
完整代碼
應用所有修復后,您的代碼應如下所示
# try not to import globally
import tkinter as tk
from tkinter import ttk
class Preferences(tk.Toplevel):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.master = master
self.title("Preferences")
self.geometry(" 90 90")
def add_tab():
master.new_tab()
self.btn_add = tk.Button(self, text="Append tab", state='normal', command=add_tab)
self.btn_add.grid(row=1, column=0, padx=(5, 0))
class Principal(tk.Tk):
def __init__(self, master=None, *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.title("Principale")
salons = ["#Radio-Evasion", "#un-autre-regard"]
self.notebook = ttk.Notebook(self)
ttk.Frame(self.notebook)
# creation Menu
self.create_menu_bar()
# Création des Tabs salons
for i, salon in enumerate(salons):
self.add_tab(salons[i])
self.notebook.pack(expand=True, fill="both")
def ouvre_gui_preferences(self):
Preferences(self)
def do_something(self):
print("Menu clicked")
def create_menu_bar(self):
menu_bar = tk.Menu(self)
menu_file = tk.Menu(menu_bar, tearoff=0)
menu_file.add_command(label="New", command=self.do_something)
menu_file.add_command(label="Save", command=self.do_something)
menu_file.add_separator()
menu_file.add_command(label="Preferences", command=self.ouvre_gui_preferences)
menu_file.add_separator()
menu_file.add_command(label="Exit", command=self.quit)
menu_bar.add_cascade(label="File", menu=menu_file)
menu_outils = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Outils", menu=menu_outils)
# menu_outils.add_command(label="Annonces", command=ouvre_annonces)
menu_outils.add_command(label="!cmd", command=self.ouvre_gui_preferences)
menu_help = tk.Menu(menu_bar, tearoff=0)
menu_help.add_command(label="About", command=self.do_something)
menu_bar.add_cascade(label="Help", menu=menu_help)
self.config(menu=menu_bar)
def add_tab(self, tab_text):
tab_name = tk.Frame(self.notebook)
self.notebook.add(tab_name, text="{}".format(tab_text))
def new_tab(self):
tab_name = tk.Frame(self.notebook)
self.notebook.add(tab_name, text="{}".format("tab_text"))
root = Principal()
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/469365.html
