【问题标题】:How do I retrieve the input into my text box from my GUI and store it in a variable?如何从我的 GUI 检索输入到我的文本框中并将其存储在变量中?
【发布时间】:2020-10-02 21:14:24
【问题描述】:

我是 Python 新手,需要一点帮助。我需要帮助来从我的文本框中检索输入并将其存储在一个变量中。这就是我目前的 GUI。基本上我只想从每个文本框中返回输入并将其存储在一个变量中。我想,因为我使用了“return like_userInput”,所以我只能将输入存储在该变量中,但它不起作用。任何信息将不胜感激。我对编程仍然非常陌生,希望您对我有任何反馈。

import tkinter as tk
from tkinter import ttk
from tkinter import *

# this is the function called when the button is clicked
def btnClickFunction():
    print('Submitted information to script.')


# this is a function to get the user input from the text input box
def getInputBoxValue():
    like_userInput = like_input.get()
    return like_userInput


# this is a function to get the user input from the text input box
def getInputBoxValue():
    comment_userInput = comment_input.get()
    return comment_userInput


# this is a function to get the user input from the text input box
def getInputBoxValue():
    follow_userInput = follow_input.get()
    return follow_userInput



root = Tk()

# This is the section of code which creates the main window
root.geometry('652x414')
root.configure(background='#00F5FF')
root.title('InstaBot')


# This is the section of code which creates a button
Button(root, text='Submit', bg='#F702D9', font=('arial', 12, 'normal'), 
command=btnClickFunction).place(x=280, y=287)

Label(root, text='Powered by Zephyr', bg='#00F5FF', foreground='#F702D9', font=('arial', 20, 
'bold')).place(x=190, y=10)


# This is the section of code which creates the a label
Label(root, text='Number of Likes', bg='#00F5FF', font=('arial', 12, 'bold')).place(x=125, y=110)


# This is the section of code which creates the a label
Label(root, text='% of Comments', bg='#00F5FF', font=('arial', 12, 'bold')).place(x=128, y=130)


# This is the section of code which creates the a label
Label(root, text='% of Follows', bg='#00F5FF', font=('arial', 12, 'bold')).place(x=138, y=150)


# This is the section of code which creates a text input box
like_input=Entry(root)
like_input.place(x=350, y=115)


# This is the section of code which creates a text input box
comment_input=Entry(root)
comment_input.place(x=350, y=135)


# This is the section of code which creates a text input box
follow_input=Entry(root)
follow_input.place(x=350, y=155)


root.mainloop()

【问题讨论】:

    标签: python tkinter input user-input return-value


    【解决方案1】:

    您已为所有获取输入函数 (getInputBoxValue) 赋予了相同的名称。您需要将它们中的每一个更改为唯一的,否则 python 每次只会执行最后一个函数定义。

    # this is a function to get the user input from the text input box
    def getInputBoxValue():
        like_userInput = like_input.get()
        return like_userInput
    
    
    # this is a function to get the user input from the text input box
    def getInputBoxValue():
        comment_userInput = comment_input.get()
        return comment_userInput
    
    
    # this is a function to get the user input from the text input box
    def getInputBoxValue():
        follow_userInput = follow_input.get()
        return follow_userInput
    

    【讨论】:

    • 我现在已经开始工作了,谢谢!如果我打印函数的返回值,它将返回一个 int,但是当我尝试将它实现到我的脚本中时,它不会以 int 的形式返回 - 它看起来更像是十六进制代码。目前正在尝试解决这个问题.
    • @zxcharyy - 欢迎您!因为此答案对您有用,请将其标记为已接受。
    猜你喜欢
    • 2013-11-12
    • 2021-02-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多