如何在python中使用tkinter将文本框条目传递给变量 [英] How to pass a textbox entry to variables using tkinter in python
问题描述
我有一些相当简单的代码来获取 urlstring 的值.
I have some fairly simple code to get values for a urlstring.
我已经查看了所有其他问题,但似乎找不到可以应用的相关答案
I have looked at all the other questions and cant seem to find a relevant answer I can apply
有些代码首先出现在我期望的 Ipython 控制台中,稍后会更改,我确实从按下按钮时的硬编码变量中获得了 Ipython 控制台中的值,但似乎无法将文本框的值放入变量中,并且然后改用它们?
Some of the code comes up first in the Ipython console which I expect and will change later and I do get a value in Ipython console from the hardcoded variables on button press but cant seem to get value of text boxes into the variables and then use them instead?
amount = '1'
cur1 = input('What Currency would you like to trade from? ')
cur2 = input('What Currency would you like to trade to? ')
cur1_1 = StringVar()
cur2_1 = StringVar()
#i = 0
#Textboxes for user input
txtcur1 = Entry(root, font="Helvetica 11 bold",bg="white", width=6, textvariable=cur1_1)
txtcur1.place(x=110, y=50)
txtcur2 = Entry(root, font="Helvetica 11 bold",bg="white", width=6, textvariable=cur2_1)
txtcur2.place(x=110, y=75)
#End
def results():
t = datetime.utcnow()
url1 = "http://www.xe.com/currencyconverter/convert/" + "?Amount=" + amount + "&From=" + cur1 + "&To=" + cur2
url2 = "http://www.xe.com/currencyconverter/convert/" + "?Amount=" + amount + "&From=" + cur2_1 + "&To=" + cur1_1
但对于我的一生,我无法将文本框中的变量放入变量 cur1_1
和 cur2_1
中,并且出现类型错误.
But for the life of me I cant get the variables from the textboxes into the variables cur1_1
and cur2_1
and am getting the typeerror.
url2 = "http://www.xe.com/currencyconverter/convert/" + "?Amount=" + amount + "&From=" + cur2_1 + "&To=" + cur1_1
TypeError: must be str, not StringVar
当我把它改成字符串时,它说必须是 3 位数长我原以为它会比这简单一点.有什么帮助吗?
When I change it to string it says must be 3 digit long I would have thought It would have been a bit simpler than this. Any help please?
此外,一旦我获得了汇率的返回值,我需要将它们转换为 十进制到 9
位并用逗号显示以供货币使用.
Also once I have the returned values for the exchange rates I need them to be converted to a decimal to 9
places and displayed with commas for monetary use .
完整代码在这里https://pastebin.com/uPWyPXMZ
推荐答案
您的问题似乎是您在使用 cur1_1
和 cur2_1
时好像它们是字符串一样应该调用它们的 StringVar.get()
方法来访问它们的字符串值.
Your problem seems to be you're using cur1_1
and cur2_1
as if they were strings when you should be calling their StringVar.get()
methods to access their string values.
一个简单的例子:
import tkinter as tk
def show_text():
label_text.set('Heh, heh, heh, you said, "' + entry_text.get() + '"')
root = tk.Tk()
entry_text = tk.StringVar()
entry = tk.Entry(root, width=10, textvariable=entry_text)
entry.pack()
button = tk.Button(root, text="Click Me", command=show_text)
button.pack()
label_text = tk.StringVar()
label = tk.Label(root, textvariable=label_text)
label.pack()
root.mainloop()
在输入框中输入文本,然后单击按钮.文本将通过与 Entry
关联的 StringVar
的 .get()
方法从输入框传输到标签.已创建.
Put text in the entry box, and click the button. The text will be transfered from the entry box to the label via the .get()
method of the StringVar
associated with the Entry
when it was created.
这篇关于如何在python中使用tkinter将文本框条目传递给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!