点击时 tkinter 显示框架
tkinter display frame on click
我正在尝试 display/include 函数 display_srv
单击菜单栏命令并获取使用按钮 选中的每个复选框的值完成
import tkinter as tk
import glob
from tkinter import *
def frame_1():
print("frame 1!")
我尝试在框架中显示的功能:
def display_srv():
frame_1 = root.LabelFrame(root, text="Liste Serveurs")
frame_1.grid(row=0, sticky='ew', padx=20, pady=20, ipadx=5, ipady=5)
path = '/home/lst/*.txt'
files=glob.glob(path)
count = 0
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
lng = root.Checkbutton(frame_1, variable = item, text=item.rstrip()).grid(row=count//10, column=count%10)
count += 1
文本文件值:
item1
item2
item3
...
item100
取值函数:
def getvalue():
print(list(lng.values()))
主脚本:
root = Tk()
menu = Menu(root)
root.geometry('700x500')
root.title("My menu")
root.config(menu=menu)
testpsimenu = Menu(menu)
menu.add_cascade(label="Test Menu", menu=testpsimenu, font=("Arial", 12))
testpsimenu.add_command(label="Select 1", font=("Arial", 10), command=frame_1)
testpsimenu.add_separator()
testpsimenu.add_command(label="Select 2", font=("Arial", 10), command=display_srv)
psimenu = Menu(menu)
menu.add_cascade(label="PSI Real", menu=psimenu, font=("Arial", 12))
psimenu.add_command(label="Select 1", font=("Arial", 10), command=frame_1)
psimenu.add_separator()
psimenu.add_command(label="Select 2", font=("Arial", 10), command=frame_1)
exitmenu = Menu(menu)
menu.add_cascade(label="Exit", menu=exitmenu, font=("Arial", 12))
exitmenu.add_command(label="Exit", command=root.quit, font=("Arial", 10))
Button(root, text='Done', font=("Arial", 12), command=getvalue).pack(side=RIGHT)
mainloop()
这是错误,当我从菜单 testpsi 中单击 Select2:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib64/python3.6/tkinter/__init__.py", line 1702, in __call__
return self.func(*args)
File "./test_menu_7.py", line 13, in display_srv
frame_1 = root.LabelFrame(root, text="Liste Serveurs")
File "/usr/lib64/python3.6/tkinter/__init__.py", line 2098, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'LabelFrame'
非常感谢您的帮助...
到目前为止我可以看到 2 个问题。
- 您需要在小部件上使用
tk.
前缀,而不是 root
。
更改这些:
root.LabelFrame
root.Checkbutton
致这些:
tk.LabelFrame
tk.Checkbutton
- 您需要将
lng
定义为全局变量,以便它可以在您的 getvalue()
函数中使用。
将此行 global lng
添加到 display_srv()
的顶部,如下所示:
def display_srv():
global lng
如果您有任何问题,请告诉我。
我正在尝试 display/include 函数 display_srv
单击菜单栏命令并获取使用按钮 选中的每个复选框的值完成
import tkinter as tk
import glob
from tkinter import *
def frame_1():
print("frame 1!")
我尝试在框架中显示的功能:
def display_srv():
frame_1 = root.LabelFrame(root, text="Liste Serveurs")
frame_1.grid(row=0, sticky='ew', padx=20, pady=20, ipadx=5, ipady=5)
path = '/home/lst/*.txt'
files=glob.glob(path)
count = 0
for file in files:
with open(file, 'r') as lst_file:
for item in lst_file:
lng = root.Checkbutton(frame_1, variable = item, text=item.rstrip()).grid(row=count//10, column=count%10)
count += 1
文本文件值:
item1
item2
item3
...
item100
取值函数:
def getvalue():
print(list(lng.values()))
主脚本:
root = Tk()
menu = Menu(root)
root.geometry('700x500')
root.title("My menu")
root.config(menu=menu)
testpsimenu = Menu(menu)
menu.add_cascade(label="Test Menu", menu=testpsimenu, font=("Arial", 12))
testpsimenu.add_command(label="Select 1", font=("Arial", 10), command=frame_1)
testpsimenu.add_separator()
testpsimenu.add_command(label="Select 2", font=("Arial", 10), command=display_srv)
psimenu = Menu(menu)
menu.add_cascade(label="PSI Real", menu=psimenu, font=("Arial", 12))
psimenu.add_command(label="Select 1", font=("Arial", 10), command=frame_1)
psimenu.add_separator()
psimenu.add_command(label="Select 2", font=("Arial", 10), command=frame_1)
exitmenu = Menu(menu)
menu.add_cascade(label="Exit", menu=exitmenu, font=("Arial", 12))
exitmenu.add_command(label="Exit", command=root.quit, font=("Arial", 10))
Button(root, text='Done', font=("Arial", 12), command=getvalue).pack(side=RIGHT)
mainloop()
这是错误,当我从菜单 testpsi 中单击 Select2:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib64/python3.6/tkinter/__init__.py", line 1702, in __call__
return self.func(*args)
File "./test_menu_7.py", line 13, in display_srv
frame_1 = root.LabelFrame(root, text="Liste Serveurs")
File "/usr/lib64/python3.6/tkinter/__init__.py", line 2098, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'LabelFrame'
非常感谢您的帮助...
到目前为止我可以看到 2 个问题。
- 您需要在小部件上使用
tk.
前缀,而不是root
。
更改这些:
root.LabelFrame
root.Checkbutton
致这些:
tk.LabelFrame
tk.Checkbutton
- 您需要将
lng
定义为全局变量,以便它可以在您的getvalue()
函数中使用。
将此行 global lng
添加到 display_srv()
的顶部,如下所示:
def display_srv():
global lng
如果您有任何问题,请告诉我。