我有一个名为 to1 的函数,它从条目小部件中获取输入 在文件中搜索它会显示带有所有相似行文本的按钮。现在 get_button_text1 函数获取按钮 ID 并提取特定按钮的文本,并将其存储在名为 btn_text 的变量中。现在的问题是,当我在新函数 find1 中调用这个全局变量 btn_text 时,它会给出一个错误 btn_text 未定义。 我分别附上代码和回溯错误。
def get_button_text1(button_id):
global btn_text
"""Function to retrieve text of a button with a given identifier"""
# print(ubtn)
btn_text = ubtn1[button_id]['text']
# dataIO.valueentry(btn_text, None, None, None)
# top1.destroy()
# print(btn_text)
return btn_text
# from home import *
def to1():
global top1
top1 = Toplevel()
string_list = [] # empty list to store the strings
c = desentry1.get()
a = str(c)
# print(a)
b = ""
if a.startswith("UC") or a.startswith("uc") or a.startswith("nibp"):
a = a.upper()
else:
a = a.capitalize()
b = a
# print(b)
search_words = b.split()
i = 0
with open("materialcode.txt", "r") as file:
lines = file.readlines()
# key = k.upper()
for line in lines:
# if word in key:
for word in search_words:
if word in line:
global buttons
# print(line)
buttons = {}
string_name = f"string1_{i + 1}" # create a variable name with a unique number
string_value = f"{line}" # create the string value
locals()[string_name] = string_value # dynamically create the variable and assign the string value
string_list.append(locals()[string_name]) # add the variable to the list
button_text = locals()[f"string1_{i + 1}"] # create the text for the button
button = ttk.Button(top1, text=button_text, command=lambda button_id=i: get_button_text1(
button_id)) # create the button with the text
button.pack() # add the button to the GUI
buttons[i] = button
# print(button)
ubtn1.update(buttons)
i += 1
break
# print(string_list)
# -----------------------------------------------------------------------------funx
def find1():
to1()
desentry1.delete(0,END)
# ov1=StringVar()
ov1=btn_text
print(ov1)
desentry1.insert(0,ov1)
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Users\admin\Desktop\Dekstop Folder\pycharm\Uni-Card\sepr.py", line 92, in find1
ov1=btn_text
^^^^^^^^
NameError: name 'btn_text' is not defined
我希望我的条目小部件文本更改为存储在 btn_text 变量中的内容。
这是针对您的问题的最小可重现示例。 它开箱即用,只包含必要的部件。
import tkinter as tk
def print_text(x):
print(btn_strings[x])
def to1():
global btn_strings
btn_strings = [] # empty list to store the strings
b = 'hello world'
search_words = b.split()
with open("search_text.txt", "r") as file:
lines = file.readlines()
i = 0
for line in lines:
for word in search_words:
if word in line:
print('found', word)
btn_strings.append(word)
button = tk.Button(root, text=word, command=lambda v=i: print_text(v))
button.pack()
i += 1
break
if __name__ == "__main__":
root = tk.Tk()
to1()
root.mainloop()
我的
search_text.txt
文件包含以下几行:
stackoverflow
hello
world
spam
text
python
只有在单击其中一个按钮时才需要从
to1()
返回,因此需要使顶层窗口像模态对话框一样。然后你需要关闭里面的顶层窗口get_button_text1()
.
下面是修改后的
get_button_text1()
和to1()
:
def get_button_text1(button_id):
global btn_text
btn_text = ubtn1[button_id]['text']
# close the toplevel
top1.destroy()
return btn_text
def to1():
global top1
top1 = Toplevel()
string_list = [] # empty list to store the strings
c = desentry1.get()
a = str(c)
# print(a)
b = ""
if a.startswith("UC") or a.startswith("uc") or a.startswith("nibp"):
a = a.upper()
else:
a = a.capitalize()
b = a
# print(b)
search_words = b.split()
i = 0
with open("materialcode.txt", "r") as file:
lines = file.readlines()
# key = k.upper()
for line in lines:
# if word in key:
for word in search_words:
if word in line:
global buttons
# print(line)
buttons = {}
string_name = f"string1_{i + 1}" # create a variable name with a unique number
string_value = f"{line}" # create the string value
locals()[string_name] = string_value # dynamically create the variable and assign the string value
string_list.append(locals()[string_name]) # add the variable to the list
button_text = locals()[f"string1_{i + 1}"] # create the text for the button
button = ttk.Button(top1, text=button_text, command=lambda button_id=i: get_button_text1(
button_id)) # create the button with the text
button.pack() # add the button to the GUI
buttons[i] = button
# print(button)
ubtn1.update(buttons)
i += 1
break
# print(string_list)
# make this toplevel like a dialog
top1.grab_set()
top1.wait_window()