[英]Tkinter update Entry 6 each time a number gets typed in Entry 1,2,3,4,5
I have a small and bad tkinter program to play MagicSquare where i have a 5x5 grid of Entry Boxes and at each end of rows and columns is a box which should count the sum of the row/column.我有一个小而坏的 tkinter 程序来玩 MagicSquare,我有一个 5x5 的输入框网格,在行和列的每一端都有一个框,它应该计算行/列的总和。 When you start the game some boxes are randomly filled with numbers.
当您开始游戏时,一些盒子会随机填充数字。 Now what i struggle with is anytime the user types a number in a not yet filled Entry or retypes the number in the Entry the box which sums should get updated.
现在我遇到的问题是用户在尚未填写的条目中键入一个数字或在条目框中重新键入数字,总和应该得到更新。 I would like to know how to update without pressing any button or activating a function due to the numbers only getting typed in.
由于只能输入数字,我想知道如何在不按任何按钮或激活 function 的情况下进行更新。
Thanks for the help!谢谢您的帮助!
I tried using another function as update_value() to.set() the stringvar() and then call the update_value() function inside the grid creation function我尝试使用另一个 function 作为 update_value() to.set() stringvar() 然后在网格创建 function 中调用 update_value() function
Here if a number randomly got assigned to Entry1 it gets taken into SumBox S1 but if it was empty and i type something into the Entry nothing happens so S1.在这里,如果一个数字随机分配给 Entry1,它会进入 SumBox S1,但如果它是空的,我在 Entry 中输入一些东西,什么也没有发生,所以 S1。
def grid_insertion_5():
global slots
global v1
global S1
grid_data(5)
frame_grid()
print(data)
slots = []
for i in range(1,26):
slots.append("E"+str(i))
print(slots)
pos = 0
for i in range(5):
for j in range(5):
insertion = data[i][j]
if insertion != 0:
slots[pos] = Entry(frame2, width=3, font="Arial 30")
slots[pos].grid(row=i, column=j, sticky=N + S + E + W, padx=5, pady=5)
slots[pos].insert(0,insertion)
slots[pos].config(state="readonly")
else:
slots[pos] = Entry(frame2, width=3, font="Arial 30",state=NORMAL)
slots[pos].grid(row=i, column=j, sticky=N + S + E + W, padx=5, pady=5)
pos+=1
v1 = StringVar()
S1 = Entry(frame2,width=3, font="Arial 30", textvariable=v1)
S1.grid(row=0,column=5,padx=5,pady=5)
root.after(1, update_value)
def update_value():
v1.set(slots[0].get())
root.update()
def test_example():
one_str = StringVar()
two_str = StringVar()
one_entry = Entry(root,textvariable=one_str)
one_entry.grid(row=2,column=0,)
two_entry = Entry(root, textvariable=two_str)
two_entry.grid(row=3,column=0)
three_entry= Entry(root)
three_entry.grid(row=4,column=0)
one_str.trace("w",update_value)
two_str.trace("w",update_value)
def update_value():
three_entry.insert(0,one_entry.get()+two_entry.get())
Look at this:看这个:
import tkinter as tk
def update_value(*args):
# Delete whatever is in the entry
three_entry.delete(0, "end")
try:
# Compute the new data
new_data = int(one_entry.get()) + int(two_entry.get())
# Put the new data in the entry
three_entry.insert(0, new_data)
except ValueError:
print("ValueError, one or both of the entries aren't integers")
root = tk.Tk()
one_str = tk.StringVar()
one_entry = tk.Entry(root, textvariable=one_str)
one_entry.grid(row=2,column=0,)
two_str = tk.StringVar()
two_entry = tk.Entry(root, textvariable=two_str)
two_entry.grid(row=3, column=0)
three_entry= tk.Entry(root)
three_entry.grid(row=4, column=0)
# each time `one_str` or `two_str` is modified call `update_value`
one_str.trace("w", update_value)
two_str.trace("w", update_value)
root.mainloop()
It uses .trace
to call update_value
each time one of the 2 entries is changed.每次更改 2 个条目之一时,它使用
.trace
调用update_value
。 Inside update_value
, I try to add the contents of one_entry
and two_entry
as int
egers and I put that inside three_entry
.在
update_value
中,我尝试将one_entry
和two_entry
的内容添加为int
,然后将其放入three_entry
中。 For more info on how .trace
works, look here .有关
.trace
如何工作的更多信息,请查看此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.