将 TKinter 输入存储到下一个函数中的字符串变量中? [英] Getting a TKinter input stored into a string variable in the next function?

查看:14
本文介绍了将 TKinter 输入存储到下一个函数中的字符串变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 TKinter 的新手,我无法弄清楚如何在 TKINTER 中存储来自文本框的输入.我已经尝试过几乎每个教程并查看了类似的帖子,但他们的想法并没有解决我的问题:/.

I am new to TKinter and I am not able to figure out how to store the input from a textbox in TKINTER. I've tried following virtually every tutorial and looked at similar posts but their ideas arent fixing my issue :/.

def cityInfoWindow(self, flightMap):
    infoWindow = Tk()
    infoWindow.geometry("500x500+100+100")
    infoWindow.title("CSAir-City Information")
    global input 
    input = StringVar()
    cityEntry = Entry(infoWindow,textvariable = input).pack()           
    okButton = Button(infoWindow, text = 'Submit', command = lambda:self.getCityInfo(infoWindow, input)).pack()

def getCityInfo(self, infoWindow, input):
    content = input.get()
    print content
    return

我试过将我的输入传递到我的函数中,但这不起作用.

Ive tried passing in my input into my function but that doesnt work.

推荐答案

有一个名为 input,尽量不要作为变量名使用.除此之外,它非常简单,

There is built-in function named input, try not to use it as a variable name. Other than that, it is pretty straight-forward,

您指定一个变量类 (StringVar()code> 在这里)用于 Entry 然后随时使用 get() 方法获取所述变量的内容.

You assign a Variable Class of your choice (StringVar() in here) for Entry then get content of said variable any time you want with get() method.

还有一个 get() 方法用于入口.这样,您无需使用 StringVar 即可获取 Entry 的内容.

Also there is a get() method for Entry. With that, you can get content of Entry without using StringVar.

下面是一个简单的例子,展示了如何做到这一点.您应该自己在代码中实现它.

Below is a simple example showing how to do it. You should implement it to your code yourself.

import tkinter as tk

def get_class():  #no need to pass arguments to functions in both cases
    print (var.get())

def get_entry(): 
    print (ent.get())


root = tk.Tk()

var = tk.StringVar()

ent = tk.Entry(root,textvariable = var)
btn1 = tk.Button(root, text="Variable Class", command=get_class)
btn2 = tk.Button(root, text="Get Method", command=get_entry)

ent.pack()
btn1.pack()
btn2.pack()

root.mainloop()

顺便说一句,下次当您发布问题时,请考虑添加完整的回溯或出了什么问题(您期望什么以及您得到什么等)而不是只说它不起作用".这样,您可能会获得更多帮助,获得更准确的答案.

By the way, next time when you post a question, please consider adding the full traceback or what went wrong (what did you expect and what did you get etc..) instead of saying "it is not working" only. With that, you will probably get more help with more precise answers.

这篇关于将 TKinter 输入存储到下一个函数中的字符串变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆