Python 使用 tkinter 按钮更新用户输入 [英] Python update user input with tkinter button
问题描述
我刚开始使用 python,但遇到了问题.我尝试了各种解决方案,但无法更新显示19"的字段.当我点击加号时,我希望它是 20,然后是 21,...当我点击时 - 它必须回到 20 、19.谁能告诉我如何解决这个问题?
I'm just starting with python and i'm having a problem. I've tried various solutions, but i can't update the field that says '19'. When i click on plus, i want it to be 20, then 21,... and when i click - it has to go back to 20 , 19. Can anybody tell me how to solve this?
from tkinter import *
def fct_tempplus():
while True:
# tekstvak_input_user = tekstvak_input_user +1
return tekstvak_input_user + 1
def fct_tempmin():
print ('ok')
window = Tk()
window.geometry("800x400") # not *
window.title("TEST")
label= Label( window, text = "Temp?")
label.place(x=350,y=175)
tempplus=Button(window, bd=10,width=10, height = 1,text="+",command=fct_tempplus,
font=("Helvetica", 12))
tempplus.place(x=500,y=150)
tempmin=Button(window, bd=10,width=10, height = 1,text="-", font=("Helvetica", 12),command=fct_tempmin)
tempmin.place(x=500,y=200)
tekstvak_input_user = Entry(window, width = 10 )
tekstvak_input_user.insert(0,19.0)
tekstvak_input_user.place(x=350 , y=200)
window.mainloop()`
推荐答案
while True
在这个程序中是不需要的.并且您必须使用 .get()
来获取函数内的值.然后你应该将它存储在一个全局变量中,将其转换为 int
或 float
.然后,只需使用 delete(0, END)
清除 Entry
小部件中的内容,然后使用 insert()
将新值插入条目
.
while True
is not needed in this program. And you have to use .get()
to get a value inside a function. And then you should store it in a globalized variable, convert it into int
or float
. Then, simply use delete(0, END)
to clear what's inside the Entry
widget and then use insert()
to insert the new value in the Entry
.
像这样:
from tkinter import *
var = 0
def fct_temp_plus():
global var
var = float(tekstvak_input_user.get())
var += 1
tekstvak_input_user.delete(0, END)
tekstvak_input_user.insert(0, var)
def fct_temp_min():
global var
var = float(tekstvak_input_user.get())
var -= 1
tekstvak_input_user.delete(0, END)
tekstvak_input_user.insert(0, var)
window = Tk()
window.geometry("800x400") # not *
window.title("TEST")
label = Label(window, text="Temp?")
label.place(x=350, y=175)
temp_plus = Button(window, bd=10, width=10, height=1, text="+", command=fct_temp_plus, font=("Helvetica", 12))
temp_plus.place(x=500, y=150)
temp_min = Button(window, bd=10, width=10, height=1, text="-", font=("Helvetica", 12), command=fct_temp_min)
temp_min.place(x=500, y=200)
tekstvak_input_user = Entry(window, width=10)
tekstvak_input_user.insert(0, 19.0)
tekstvak_input_user.place(x=350, y=200)
window.mainloop()
注意:你应该总是import tkinter as tk
.
像这样:
import tkinter as tk
var = 0
def fct_temp_plus():
global var
var = float(tekstvak_input_user.get())
var += 1
tekstvak_input_user.delete(0, tk.END)
tekstvak_input_user.insert(0, var)
def fct_temp_min():
global var
var = float(tekstvak_input_user.get())
var -= 1
tekstvak_input_user.delete(0, tk.END)
tekstvak_input_user.insert(0, var)
window = tk.Tk()
window.geometry("800x400")
window.title("TEST")
label = tk.Label(window, text="Temp?")
label.place(x=350, y=175)
temp_plus = tk.Button(window, bd=10, width=10, height=1, text="+", command=fct_temp_plus, font=("Helvetica", 12))
temp_plus.place(x=500, y=150)
temp_min = tk.Button(window, bd=10, width=10, height=1, text="-", font=("Helvetica", 12), command=fct_temp_min)
temp_min.place(x=500, y=200)
tekstvak_input_user = tk.Entry(window, width=10)
tekstvak_input_user.insert(0, 19.0)
tekstvak_input_user.place(x=350, y=200)
window.mainloop()
这篇关于Python 使用 tkinter 按钮更新用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!