python - Tkinter button arguments
问题描述
I've started learning python recently. I have little experience in gui and wanted to learn some more. With little research, I've found tkinter to make it real. As a project, decided to make the game mastermind.
What I'm doing is putting some rows with OptionMenu with the available colors. On the right I've put a confirm button, that has the command "validate" set. The thing is that I want to pass different values to each button (the frame and the row) with the following code, the problem I have is that it says that the row is always 9.
I've thought a possible solution, that is to reset at the end y=0 and every time I press the button add 1. But I think it should be simpler.
from tkinter import *
def selectedColor(var):
print("Value is: " + var)
def validate(frame, row):
# pass
print("Frame: " + str(frame) + " row: " + str(row))
rowToValidate = frame.nametowidget("." + str(row))
for x in range(0,4):
columnValue = frame.nametowidget("." + str(x) + str(row))
print(columnValue)
master = Tk()
OPTIONS
= [
"RED",
"BLUE",
"YELLOW",
"VIOLET",
"ORANGE",
"GREEN",
"BROWN",
"WHITE",
"BLACK"
]
colorQuantity = 4
tries = 10
for y in range(0, tries):
frame = Frame(master, name=str(y))
isEnabled = "disabled" if y != 0 else "normal"
for x in range(0,colorQuantity):
variable = StringVar(frame, name=str(x)+str(y))
variable.set("Choose color") # default value
# print(str(variable))
w = OptionMenu(frame, variable, *OPTIONS, command=selectedColor)
w.configure(state=isEnabled)
w.pack(side=LEFT)
confirmButton = Button(frame, text="Confirm", command= lambda: validate(frame, y+1))
confirmButton.configure()
confirmButton.pack(side=TOP)
print(str(frame))
frame.pack(side=TOP)
master.mainloop()
解决方案
我会将 OptionMenu 变量保存在列表中,以便更轻松地检索它们的值。通过在命令选项中使用partial
( link ),您可以确保每一行都获得正确的行号作为输入。
请参阅下面的代码(我还在下拉列表中添加了一些格式):
from tkinter import *
from functools import partial
def selectedColor(var):
print("Value is: " + var)
def validate(args):
frame = args[0]
row = args[1]
for x in range(0,4):
print(rows[row][x].get())
master = Tk()
OPTIONS = [
"RED",
"BLUE",
"YELLOW",
"VIOLET",
"ORANGE",
"GREEN",
"BROWN",
"WHITE",
"BLACK"
]
colorQuantity = 4
tries = 10
rows =[]
for y in range(0, tries):
frame = Frame(master, name=str(y))
isEnabled = "disabled" if y != 0 else "normal"
row = []
for x in range(0,colorQuantity):
variable = StringVar(frame, name=str(x)+str(y))
variable.set("Choose color") # default value
# print(str(variable))
row.append(variable)
w = OptionMenu(frame, variable, *OPTIONS, command=selectedColor)
w.configure(width=len("Choose color"))
w.configure(state=isEnabled)
w.pack(side=LEFT)
rows.append(row)
confirmButton = Button(frame, text="Confirm", command=partial(validate,(frame, y)))
confirmButton.configure()
confirmButton.pack(side=TOP)
print(str(frame))
frame.pack(side=TOP)
master.mainloop()
推荐阅读
- javascript - 在 JS 中获取 MM/DD/YYYY 格式的当前日期作为字符串?
- reactjs - 如何调试新克隆的 React Native 应用程序
- c - 在 C 中定义字符串的细节是什么?
- css - Vuetfiy - 替代 flexbox 浮动
- bash - 对于带参数的 bash
- java - 有没有办法可以将所有大于前一个的数字相加?
- javascript - Angular v9 问题:无法解析 h 的所有参数:(?, ?, ?)
- rust - 将相同变量绑定到共享特征的不同类型的模式
- javascript - 创建一个函数来切片而不使用 slice()
- python - 如何通过 tkinter 画布动作中的变量 = false 条件取消正在运行的函数