Python 3,Tkinter,如何更新按钮文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32615440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 3, Tkinter, How to update button text
提问by Mrchooch
Im trying to make it so that when the user clicks a button, it becomes "X" or "0" (Depending on their team). How can I make it so that the text on the button is updated? My best idea so far has been to delete the buttons then print them again, but that only deletes one button. Here's what I have so far:
我试图做到这一点,当用户点击一个按钮时,它会变成“X”或“0”(取决于他们的团队)。我怎样才能使按钮上的文本更新?到目前为止,我最好的想法是删除按钮然后再次打印它们,但这只会删除一个按钮。这是我到目前为止所拥有的:
from tkinter import *
BoardValue = ["-","-","-","-","-","-","-","-","-"]
window = Tk()
window.title("Noughts And Crosses")
window.geometry("10x200")
v = StringVar()
Label(window, textvariable=v,pady=10).pack()
v.set("Noughts And Crosses")
def DrawBoard():
for i, b in enumerate(BoardValue):
global btn
if i%3 == 0:
row_frame = Frame(window)
row_frame.pack(side="top")
btn = Button(row_frame, text=b, relief=GROOVE, width=2, command = lambda: PlayMove())
btn.pack(side="left")
def PlayMove():
BoardValue[0] = "X"
btn.destroy()
DrawBoard()
DrawBoard()
window.mainloop()
采纳答案by David
The Button widget, just like your Label, also has a textvariable=
option. You can use StringVar.set()
to update the Button. Minimal example:
按钮小部件,就像您的标签一样,也有一个textvariable=
选项。您可以使用StringVar.set()
更新按钮。最小的例子:
import tkinter as tk
root = tk.Tk()
def update_btn_text():
btn_text.set("b")
btn_text = tk.StringVar()
btn = tk.Button(root, textvariable=btn_text, command=update_btn_text)
btn_text.set("a")
btn.pack()
root.mainloop()
回答by WhyAreYouReadingThis
btn
is just a dictionary of values, lets see what comes up then:
btn
只是一个值字典,让我们看看接下来会发生什么:
#lets do another button example
Search_button
<tkinter.Button object .!button>
#hmm, lets do dict(Search_button)
dict(Search_button)
{'activebackground': 'SystemButtonFace', 'activeforeground':
'SystemButtonText', 'anchor': 'center', 'background': 'SystemButtonFace',
'bd': <pixel object: '2'>, 'bg': 'SystemButtonFace', 'bitmap': '',
'borderwidth': <pixel object: '2'>, 'command': '100260920point', 'compound':
'none', 'cursor': '', 'default': 'disabled', 'disabledforeground':
'SystemDisabledText', 'fg': 'SystemButtonText', 'font': 'TkDefaultFont',
'foreground': 'SystemButtonText', 'height': 0, 'highlightbackground':
'SystemButtonFace', 'highlightcolor': 'SystemWindowFrame',
'highlightthickness': <pixel object: '1'>, 'image': '', 'justify': 'center',
'overrelief': '', 'padx': <pixel object: '1'>, 'pady': <pixel object: '1'>,
'relief': 'raised', 'repeatdelay': 0, 'repeatinterval': 0, 'state':
'normal', 'takefocus': '', 'text': 'Click me for 10 points!',
'textvariable': '', 'underline': -1, 'width': 125, 'wraplength': <pixel
object: '0'>}
#this will not work if you have closed the tkinter window
As you can see, it is a large dictionary of values, so if you want to change any button, simply do:
如您所见,它是一个大型值字典,因此如果您想更改任何按钮,只需执行以下操作:
Button_that_needs_to_be_changed["text"] = "new text here"
Thats it really!
原来如此!
It will automatically change the text on the button, even if you are on IDLE!
即使您处于空闲状态,它也会自动更改按钮上的文本!
回答by mesutpiskin
use myButtonObject["text"] = "Hello World"
用 myButtonObject["text"] = "Hello World"
python 3
蟒蛇 3
from tkinter import *
btnMyButton = Button(text="Im Button", command=onClick)
btnMyButton["text"] = "Im not button"
python 2
蟒蛇2
import Tkinter as tk
btnMyButton = tk.Button(text="Im Button", command=onClick)
btnMyButton["text"] = "Im not button"
回答by ChaosPredictor
Another way is by btn.configure(text="new text"), like this:
另一种方法是通过 btn.configure(text="new text"),像这样:
import tkinter as tk
root = tk.Tk()
def update_btn_text():
if(btn["text"]=="a"):
btn.configure(text="b")
else:
btn.configure(text="a")
btn = tk.Button(root, text="a", command=update_btn_text)
btn.pack()
root.mainloop()
回答by Trieu Nguyen
To sum up this thread: button.config
and button.configure
both work fine!
综上所述这个线程:button.config
和button.configure
都工作得不错!
button.config(text="hello")
or
或者
button.configure(text="hello")
回答by ForceVII
I think that code will be useful for you.
我认为该代码对您有用。
import tkinter
from tkinter import *
#These Necessary Libraries
App = Tk()
App.geometry("256x192")
def Change():
Btn1.configure(text=Text.get()) # Changes Text As Entry Message.
Ent1.delete(first=0, last=999) # Not necessary. For clearing Entry.
Btn1 = Button(App, text="Change Text", width=16, command=Change)
Btn1.pack()
Text = tkinter.StringVar() # For Pickup Text
Ent1 = Entry(App, width=32, bd=3, textvariable=Text) #<-
Ent1.pack()
App.mainloop()
回答by Rahulsdhar
from tkinter import *
BoardValue = ["-","-","-","-","-","-","-","-","-"]
window = Tk()
window.title("Noughts And Crosses")
window.geometry("10x200")
v = StringVar()
Label(window, textvariable=v,pady=10).pack()
v.set("Noughts And Crosses")
btn=[]
class BoardButton():
def __init__(self,row_frame,b):
global btn
self.position= len(btn)
btn.append(Button(row_frame, text=b, relief=GROOVE, width=2,command=lambda: self.callPlayMove()))
btn[self.position].pack(side="left")
def callPlayMove(self):
PlayMove(self.position)
def DrawBoard():
for i, b in enumerate(BoardValue):
global btn
if i%3 == 0:
row_frame = Frame(window)
row_frame.pack(side="top")
BoardButton(row_frame,b)
#btn.append(Button(row_frame, text=b, relief=GROOVE, width=2))
#btn[i].pack(side="left")
def UpdateBoard():
for i, b in enumerate(BoardValue):
global btn
btn[i].config(text=b)
def PlayMove(positionClicked):
if BoardValue[positionClicked] == '-':
BoardValue[positionClicked] = "X"
else:
BoardValue[positionClicked] = '-'
UpdateBoard()
DrawBoard()
window.mainloop()