Python 3.x 如何对标签或按钮进行永久性输入更改

Python 3.x 如何对标签或按钮进行永久性输入更改,python-3.x,tkinter,tkinter-entry,Python 3.x,Tkinter,Tkinter Entry,我做了一个示例程序,介绍它的大致情况。。我的目标是让数据输入永久写入按钮,以便在我再次运行程序时更新当前价格 from tkinter import* import tkinter as tk import tkinter.simpledialog def changeP1(event): btnT4=tk.Button(root,text='Updating...',width=10,bg='green') btnT4.grid(in_=root,row=1,column=2

我做了一个示例程序,介绍它的大致情况。。我的目标是让数据输入永久写入按钮,以便在我再次运行程序时更新当前价格

from tkinter import*
import tkinter as tk
import tkinter.simpledialog

def changeP1(event):
    btnT4=tk.Button(root,text='Updating...',width=10,bg='green')
    btnT4.grid(in_=root,row=1,column=2)
    btnT4.bind('<1>',changeP1)
    askC1=tk.simpledialog.askfloat('Updating...','What is the current price?')
    btnT4=tk.Button(root,text=('RM {:,.2f}'.format(askC1)),width=10)
    btnT4.grid(in_=root,row=1,column=2)
    btnT4.bind('<1>',changeP1)
def changeP2(event):
    btnT4=tk.Button(root,text='Updating...',width=10,bg='green')
    btnT4.grid(in_=root,row=2,column=2)
    btnT4.bind('<1>',changeP2)
    askC2=tk.simpledialog.askfloat('Updating...','What is the current price?')
    btnT4=tk.Button(root,text=('RM {:,.2f}'.format(askC2)),width=10)
    btnT4.grid(in_=root,row=2,column=2)
    btnT4.bind('<1>',changeP2)
def changeP3(event):
    btnT4=tk.Button(root,text='Updating...',width=10,bg='green')
    btnT4.grid(in_=root,row=3,column=2)
    btnT4.bind('<1>',changeP3)
    askC3=tk.simpledialog.askfloat('Updating...','What is the current price?')
    btnT4=tk.Button(root,text=('RM {:,.2f}'.format(askC3)),width=10)
    btnT4.grid(in_=root,row=3,column=2)
    btnT4.bind('<1>',changeP3)

root=Tk()
Title=['Item','Unit','Price']
Item=['Kopi O','Teh O','Teh Tarik']
Unit= '1 cup'
Price=[1,0.9,1.2]
cl=[0,1,2]
rw=[1,2,3]

for i in range(3):
    btnT1=tk.Button(root,text=Title[i],width=10,bg='yellow')
    btnT1.grid(in_=root,row=0,column=cl[i])

for x in range(3):
    btnT2=tk.Button(root,text=Item[x],width=10)
    btnT2.grid(in_=root,row=rw[x],column=0)

for y in range(3):
    btnT3=tk.Button(root,text=Unit,width=10)
    btnT3.grid(in_=root,row=rw[y],column=1)             

for z in range(3):
    btnT4=tk.Button(root,text=('RM {:,.2f}'.format(Price[z])),width=10)
    btnT4.grid(in_=root,row=rw[z],column=2)
    if z in range(0,1):
        btnT4.bind('<1>',changeP1)
    if z in range(1,2):
        btnT4.bind('<1>',changeP2)
    if z in range(2,3):
        btnT4.bind('<1>',changeP3)

root.mainloop()
从tkinter导入*
将tkinter作为tk导入
导入tkinter.simpledialog
def changeP1(事件):
btnT4=tk.Button(根,text='updateing…',width=10,bg='green')
btnT4.grid(in=根,行=1,列=2)
btnT4.bind(“”,changeP1)
askC1=tk.simpledialog.askfloat('更新…','当前价格是多少?'))
btnT4=tk.Button(根,文本=('RM{:,.2f}'。格式(askC1)),宽度=10)
btnT4.grid(in=根,行=1,列=2)
btnT4.bind(“”,changeP1)
def changeP2(事件):
btnT4=tk.Button(根,text='updateing…',width=10,bg='green')
btnT4.grid(in=根,行=2,列=2)
btnT4.bind(“”,changeP2)
askC2=tk.simpledialog.askfloat(‘更新…’,‘当前价格是多少?’)
btnT4=tk.Button(根,文本=('RM{:,.2f}'。格式(askC2)),宽度=10)
btnT4.grid(in=根,行=2,列=2)
btnT4.bind(“”,changeP2)
def changeP3(事件):
btnT4=tk.Button(根,text='updateing…',width=10,bg='green')
btnT4.grid(in=根,行=3,列=2)
btnT4.bind(“”,changeP3)
askC3=tk.simpledialog.askfloat('更新…','当前价格是多少?'))
btnT4=tk.Button(根,文本=('RM{:,.2f}'。格式(askC3)),宽度=10)
btnT4.grid(in=根,行=3,列=2)
btnT4.bind(“”,changeP3)
root=Tk()
标题=[“项目”、“单位”、“价格”]
项目=['Kopi O','Teh O','Teh Tarik']
单位='1杯'
价格=[1,0.9,1.2]
cl=[0,1,2]
rw=[1,2,3]
对于范围(3)中的i:
btnT1=tk.Button(根,文本=标题[i],宽度=10,bg='yellow')
btnT1.grid(in=root,row=0,column=cl[i])
对于范围(3)内的x:
btnT2=tk.按钮(根,文本=项[x],宽度=10)
btnT2.grid(in=root,row=rw[x],column=0)
对于范围(3)内的y:
btnT3=tk.按钮(根,文本=单位,宽度=10)
btnT3.grid(in=root,row=rw[y],column=1)
对于范围(3)内的z:
btnT4=tk.Button(根,文本=('RM{:,.2f}'。格式(价格[z]),宽度=10)
btnT4.grid(in=root,row=rw[z],column=2)
如果z在范围(0,1)内:
btnT4.bind(“”,changeP1)
如果z在范围(1,2)内:
btnT4.bind(“”,changeP2)
如果z在范围(2,3)内:
btnT4.bind(“”,changeP3)
root.mainloop()
如果有什么办法让这更简单的话

您有两个选项(据我所知),因为您正在动态创建按钮。两个选项只需要一个功能

如果希望使用
bind
,则可以使用
event.widget

def onChange(event):
    ans = tk.simpledialog.askfloat('Updating...','What is the current price?')
    if ans: # checks if None is returned when clicking cancel
        event.widget.config(text='RM {:,.2f}'.format(ans))
因此,在循环中只有一个bind
btnT4.bind(“”,onChange)

或者,使用按钮的“命令”属性指定按下按钮时要调用的函数。对按钮使用命令通常比绑定更具python风格

这要求您还创建一个列表来存储按钮,以允许函数知道要更改哪个小部件

btn_list = [] # create an empty list for the buttons

# Your for loop will look like this the command parameter instead 
# and append the button to the list
for z in range(3):
    btnT4=tk.Button(root,text=('RM {:,.2f}'.format(Price[z])),width=10,\
                    command=lambda i=z: onChange(i))
    btnT4.grid(in_=root,row=rw[z],column=2)
    btn_list.append(btnT4)
lambda
z
的值传递到
onChange
函数中,以创建该按钮的唯一调用。“z”的值相对于列表中按钮的索引位置

调用
onChange
函数时将请求新的输入,如果有效,将使用索引更新列表中存储的按钮对象

# Your change function will look like this
def onChange(i):
    ans = tk.simpledialog.askfloat('Updating...','What is the current price?')
    if ans:
        btn_list[i].config(text='RM {:,.2f}'.format(ans))

你试过StringVar吗?我很抱歉,但是我不得不说,这个代码非常糟糕……你知道按钮有一个
命令
属性吗?例如,
按钮(root,text=“h”,command=changeP3)
这意味着您不需要将命令绑定到它。您也可以只使用一个命令,通过将按钮传递给命令来更改所有按钮。要获得更多帮助,请访问我的网站。我从您的帖子thx中学到了很多,让我再次使用thx。。这很有帮助:)