python - Tkinter:如何让按钮触发以影响另一个按钮
问题描述
我正在创建一个旨在模拟某种在线商店的 GUI。
任务的一部分是有一个按钮,该按钮将生成一个 HTML 文档,其中包含用户选择的产品类别的图像。
下面我提供了我的四个单选按钮以及它们的 IntVar 和命令。
每个 RadioButton 命令都执行相同的操作,但从不同的网站提取信息,因此为简洁起见,我只提供了拖鞋类别的命令。
home_hobbies = Tk()
status = IntVar()
def show_slippers():
#open downloaded file to extract info
slipperfile = open('slippers.html','r',encoding = 'utf-8').read()
prices = findall("<span.*value'>(.*)</span>", slipperfile) #regex prices
titles = findall('<h3.*body ">\n\s*(.*)', slipperfile) #regex titles
select_categ.config(state=NORMAL) #make the text box edit-able
select_categ.delete(1.0, END) #delete any text already in the text box
#for loop to find first five items and print them
for i in range(5):
title = titles[i]
price = prices[i]
result = str(i+1) + ". " + title + ' - $' + price + "\n"
select_categ.insert(END, result) #write list of products
select_categ.config(state=DISABLED) #make sure the user can't edit the text box
slippers = Radiobutton(home_hobbies, command = show_slippers, indicator = 'off', variable = status, value = 1, text = 'Winter Slippers')
diy = Radiobutton(home_hobbies, command = show_diy, indicator = 'off', variable = status, value = 2, text = "DIY Supplies")
#newstock radiobuttons
sports = Radiobutton(home_hobbies, command = show_sports, indicator = 'off', variable = status, value = 3, text = "Pool Toys")
novelties = Radiobutton(home_hobbies, command = show_novelties, indicator = 'off', variable = status, value = 4, text = "Novelty Items")
select_categ = Text(home_hobbies, wrap = WORD, font = content_font, bg = widgetbg, fg = fontcolour, width = 40)
上面,我还提供了生成 Text 小部件的代码行,因为它可能有助于回答我的问题(尽管阅读了 effbot 页面大约 20 次,但我对这个小部件的理解并不深入)。
我现在有一个不同的按钮,其任务是使用它自己的命令“show_img”生成一个 HTML 文档:
htmlshow = Button(home_hobbies, text = "View Product Images", command = show_img)
我正在尝试使 show_img() 命令工作,这样我就有了 HTML 编码的序言,然后,根据选择的单选按钮,该函数将用相应的信息替换代码部分:
def show_img():
#in this section I write my HTML code which includes replaceable sections such as "image1" and source_url
if slipper_trig:
table = table.replace("source_url", '<a href = "https://www.etsy.com/au/search?q=slippers"> Etsy - Shop Unique Gifts for Everyone</a>')
imgfile = open('slippers.html', 'r', encoding = 'utf-8').read()
images = findall('<img\n*.*image\n*\s*src="(.*)"', imgfile)
for i in range(5):
image = images[i]
table = table.replace("image"+str(i+1), image)
我尝试将 BooleanVar 添加到我的单选按钮的命令中,如下所示:
slipper_trig = False
diy_trig = False
pool_trig = False
novelty_trig = False
#Function for the product category buttons
#
def show_slippers():
#make selected category true and change all others to false
slipper_trig = True
diy_trig = False
pool_trig = False
novelty_trig = False
作为区分类别的一种方式,但在“show_slippers”函数中将“slipper_trig”定义为真后,GUI 显然不记得“slipper_trig”的值。
也许我需要尝试将“show_img”命令集成到定义 RadioButtons 的原始函数中?也许我应该弄清楚如何确定文本框中显示的内容所选择的类别?
任何意见,将不胜感激。
谢谢!
解决方案
您没有针对您的问题显示最少的工作代码,因此我只能展示一些最小的示例Button
并RadioButton
展示如何使用这些小部件。
不知道你用过没有command=function_name
。
顺便说一句:它必须是函数的名称,没有Button
()
我不知道您是否曾经从// assigned to中.get()
获取价值。StringVar
Intvar
BooleanVar
RadioButtons
编辑我添加Checkbutton
,因为您可能需要它而不是Radiobutton
import tkinter as tk
# --- functions ---
def on_click():
selected = result_var.get()
print('selected:', selected)
if selected == 'hello':
print("add HELLO to html")
elif selected == 'bye':
print("add BYE to html")
else:
print("???")
print('option1:', option1_var.get()) # 1 or 0 if you use IntVar
print('option2:', option2_var.get()) # 1 or 0 if you use IntVar
if option1_var.get() == 1:
print("add OPTION 1 to html")
if option2_var.get() == 1:
print("add OPTION 2 to html")
# --- main ---
root = tk.Tk()
result_var = tk.StringVar(root, value='hello')
rb1 = tk.Radiobutton(root, text="Hello World", variable=result_var, value='hello')
rb1.pack()
rb2 = tk.Radiobutton(root, text="Good Bye", variable=result_var, value='bye')
rb2.pack()
option1_var = tk.IntVar(root, value=0)
opt1 = tk.Checkbutton(root, text='Option 1', variable=option1_var)
opt1.pack()
option2_var = tk.IntVar(root, value=0)
opt2 = tk.Checkbutton(root, text='Option 2', variable=option2_var)
opt2.pack()
button = tk.Button(root, text='OK', command=on_click)
button.pack()
root.mainloop()
推荐阅读
- google-chrome - CreateProcess 返回的句柄与启动的 Chrome.exe 不同
- ajax - 从 Ajax 调用 Spring Boot Rest API 时出现 CORS 错误
- python - 抓取 Tkinter 光标
- java - 我无法在 Java 的 if 语句中访问实例变量
- ruby - 从 Nosql 数据的哈希中获取数据的通用 ruby 方法实现
- javascript - 反应道具中的误导性流类型错误
- c# - 使用 LdapConnection 连接到 AD/LDS 的凭据无效
- laravel - Eloquent 连接返回空数据集
- sql - 使用 WITH 条件,但代码似乎抛出语法错误,怎么了?
- polymer - @polymer/lit-element 已被弃用