如何从 GUI 获取多个文本条目并在主 python 脚本中使用它们? [英] How to get multiple text entries from GUI and use those in a main python script?

查看:31
本文介绍了如何从 GUI 获取多个文本条目并在主 python 脚本中使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 文件,可以提取推文,获取它们的地理坐标和情绪,最后将这些推文/情绪绘制为地图上的彩色圆圈.

I have a python file that extracts tweets, get their geo-coordinates and sentiment and finally plots those tweets/sentiment as colored circles on a map.

需要以下输入(文本条目)才能使其工作:每个输入提示旁边还显示了一个示例用户输入:

The following inputs (text entries) are needed to get it to work: An example user-input is also shown next to each input prompt:

 Enter the maximum number of tweets: *100*

 Do you want to search by topic? type: y or n: *y*

 Enter topic: *MoSalah*

 Enter visulaization/projection type:  
 1. Mercator 
 2. orthographic 
 3. melloweide 
 >>  *Mercator* 

 Zoom in to a conteninent of choice:  
 1. world 
 2. africa 
 3. asia 
 4. north america 
 5. south america 
 6. europe 
 7. usa 
 >>  *world*

 Enter symbol shape: 
 1. square 
 2. circle 
 >> *circle*

现在为了让用户体验更令人兴奋,我想构建一个简单的 GUI 来询问用户所有这些输入并将它们存储到各自的变量中,但我不知道如何创建一个更重要的将 GUI 链接到其背后运行的 Python 代码.

Now in order to make the user experience more exciting, I want to build a simple GUI that asks the user for all those inputs and store them into their respective variables, but I don't know how to create one and more importantly link the GUI to the python code running behind it.

对于上面显示的每个必需输入,我是否必须具有单独的检索功能?例如,这是如何检索最大数的函数.推文应该看起来像使用 tkinter GUI :

Do I have to have separate retrieval function for each one of the required inputs shown above? For example, is this how the retrieval function for max no. of tweets should look like using a tkinter GUI :

from tkinter import * 
root = Tk()

root.geometry('200x100')

# Retrieve to get input form user and store it in a variable

# Retrieve maximum number of tweets

def retrieveMaxTweets():
    maxTweets = textBox.get()

    return maxTweets 

textBox = Text(root, height = 2, width = 10)
textBox.pack()

buttonComment = Button(root, height=1, width=10, text='Enter max no. of tweets', command = lambda: retrieveMaxTweets())

buttonComment.pack()

mainloop()

然后在我最初要求限制的代码部分,我这样做:

And then in the part of the code where I initially asked for the limit, I do this:

limit = retrieveMaxTweets()

而不是这个:

limit = int(input(" Enter the maximum number of tweets: "))

推荐答案

您可以将不同 GUI问题"的结果存储到字典中,以供代码的其他部分使用.这样你就只有一个函数来收集/验证/存储"响应.

You could store the results of the different GUI 'questions' in to a dictionary ready for the other parts of the code to use. That way you would only have one function that 'collects/validates/stores' the responses.

例如

import tkinter as tk

class App(tk.Frame):
    def __init__(self,master=None,**kw):
        #Create a blank dictionary
        self.answers = {}
        tk.Frame.__init__(self,master=master,**kw)

        tk.Label(self,text="Maximum number of Tweets").grid(row=0,column=0)
        self.question1 = tk.Entry(self)
        self.question1.grid(row=0,column=1)

        tk.Label(self,text="Topic").grid(row=1,column=0)
        self.question2 = tk.Entry(self)
        self.question2.grid(row=1,column=1)

        tk.Button(self,text="Go",command = self.collectAnswers).grid(row=2,column=1)


    def collectAnswers(self):
        self.answers['MaxTweets'] = self.question1.get()
        self.answers['Topic'] = self.question2.get()
        functionThatUsesAnswers(self.answers)

def functionThatUsesAnswers(answers):
    print("Maximum Number of Tweets ", answers['MaxTweets'])
    print("Topic ", answers['Topic'])


if __name__ == '__main__':
    root = tk.Tk()
    App(root).grid()
    root.mainloop()

按下按钮后,每个答案"都会添加到字典中,然后将其传递给执行代码主要部分的函数.

When the button is pressed, each of the 'answers' are added to a dictionary which is then passed to the function that does the main part of your code.

这篇关于如何从 GUI 获取多个文本条目并在主 python 脚本中使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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