首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Tkinter从输入框中获取文本

Tkinter从输入框中获取文本
EN

Stack Overflow用户
提问于 2021-01-20 13:45:10
回答 2查看 203关注 0票数 1

我想知道如何从Tkinter输入框中获取文本。我用这个网站做了UI:https://visualtk.com。但是现在我不知道如何获得输入的文本。我在这里粘贴我的脚本:

代码语言:javascript
运行
复制
import tkinter as tk
import tkinter.font as tkFont

class App:
    def __init__(self, root):
        #setting title
        root.title("Login")
        #setting window size
        width=497
        height=214
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(alignstr)
        root.resizable(width=False, height=False)

        GLabel_129=tk.Label(root)
        GLabel_129["bg"] = "#ff8c00"
        ft = tkFont.Font(family='Times',size=28)
        GLabel_129["font"] = ft
        GLabel_129["fg"] = "#ffffff"
        GLabel_129["justify"] = "left"
        GLabel_129["text"] = "Login:"
        GLabel_129.place(x=0,y=10,width=497,height=66)

        GLabel_333=tk.Label(root)
        GLabel_333["bg"] = "#1e9fff"
        ft = tkFont.Font(family='Times',size=10)
        GLabel_333["font"] = ft
        GLabel_333["fg"] = "#01aaed"
        GLabel_333["justify"] = "center"
        GLabel_333["text"] = ""
        GLabel_333.place(x=0,y=80,width=497,height=15)

        GLineEdit_552=tk.Entry(root)
        GLineEdit_552["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=14)
        GLineEdit_552["font"] = ft
        GLineEdit_552["fg"] = "#333333"
        GLineEdit_552["justify"] = "left"
        GLineEdit_552["text"] = ""
        GLineEdit_552.place(x=110,y=100,width=375,height=32)

        GLineEdit_314=tk.Entry(root)
        GLineEdit_314["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=14)
        GLineEdit_314["font"] = ft
        GLineEdit_314["fg"] = "#333333"
        GLineEdit_314["justify"] = "left"
        GLineEdit_314["text"] = ""
        GLineEdit_314.place(x=110,y=140,width=375,height=32)
        GLineEdit_314["show"] = "*"

        GLabel_702=tk.Label(root)
        ft = tkFont.Font(family='Times',size=14)
        GLabel_702["font"] = ft
        GLabel_702["fg"] = "#ff0000"
        GLabel_702["justify"] = "left"
        GLabel_702["text"] = "Username:"
        GLabel_702.place(x=10,y=100,width=100,height=32)

        GLabel_648=tk.Label(root)
        ft = tkFont.Font(family='Times',size=14)
        GLabel_648["font"] = ft
        GLabel_648["fg"] = "#ff0000"
        GLabel_648["justify"] = "left"
        GLabel_648["text"] = "Password:"
        GLabel_648.place(x=10,y=140,width=100,height=32)
if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()

是否有任何命令或函数来获取文本?或者我需要一个按钮才能输入文本?

EN

回答 2

Stack Overflow用户

发布于 2022-08-21 00:16:44

self.GLineEdit_....

  • Added

  • 将所有条目元素创建为.get()按钮,以便使用.get()方法运行comands.

  • Accessed条目元素(以前声明为self.)。

重新编写的代码:

代码语言:javascript
运行
复制
    import tkinter as tk
    import tkinter.font as tkFont
    
    class App:
        def __init__(self, root):
            #setting title
            root.title("Login")
            #setting window size
            width=497
            height=214
            screenwidth = root.winfo_screenwidth()
            screenheight = root.winfo_screenheight()
            alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
            root.geometry(alignstr)
            root.resizable(width=False, height=False)
    
            GLabel_129=tk.Label(root)
            GLabel_129["bg"] = "#ff8c00"
            ft = tkFont.Font(family='Times',size=28)
            GLabel_129["font"] = ft
            GLabel_129["fg"] = "#ffffff"
            GLabel_129["justify"] = "left"
            GLabel_129["text"] = "Login:"
            GLabel_129.place(x=0,y=10,width=497,height=66)
    
            GLabel_333=tk.Label(root)
            GLabel_333["bg"] = "#1e9fff"
            ft = tkFont.Font(family='Times',size=10)
            GLabel_333["font"] = ft
            GLabel_333["fg"] = "#01aaed"
            GLabel_333["justify"] = "center"
            GLabel_333["text"] = ""
            GLabel_333.place(x=0,y=80,width=497,height=15)
    
            self.GLineEdit_552=tk.Entry(root)
            self.GLineEdit_552["borderwidth"] = "1px"
            ft = tkFont.Font(family='Times',size=14)
            self.GLineEdit_552["font"] = ft
            self.GLineEdit_552["fg"] = "#333333"
            self.GLineEdit_552["justify"] = "left"
            self.GLineEdit_552["text"] = ""
            self.GLineEdit_552.place(x=110,y=100,width=375,height=32)
    
            self.GLineEdit_314=tk.Entry(root)
            self.GLineEdit_314["borderwidth"] = "1px"
            ft = tkFont.Font(family='Times',size=14)
            self.GLineEdit_314["font"] = ft
            self.GLineEdit_314["fg"] = "#333333"
            self.GLineEdit_314["justify"] = "left"
            self.GLineEdit_314["text"] = ""
            self.GLineEdit_314.place(x=110,y=140,width=375,height=32)
            self.GLineEdit_314["show"] = "*"
    
            GLabel_702=tk.Label(root)
            ft = tkFont.Font(family='Times',size=14)
            GLabel_702["font"] = ft
            GLabel_702["fg"] = "#ff0000"
            GLabel_702["justify"] = "left"
            GLabel_702["text"] = "Username:"
            GLabel_702.place(x=10,y=100,width=100,height=32)
    
            GLabel_648=tk.Label(root)
            ft = tkFont.Font(family='Times',size=14)
            GLabel_648["font"] = ft
            GLabel_648["fg"] = "#ff0000"
            GLabel_648["justify"] = "left"
            GLabel_648["text"] = "Password:"
            GLabel_648.place(x=10,y=140,width=100,height=32)
    
            GButton_858=tk.Button(root)
            GButton_858["bg"] = "#f0f0f0"
            ft = tkFont.Font(family='Times',size=10)
            GButton_858["font"] = ft
            GButton_858["fg"] = "#000000"
            GButton_858["justify"] = "center"
            GButton_858["text"] = "Button to do anything"
            GButton_858.place(x=200,y=180,width=120,height=25)
            GButton_858["command"] = self.GButton_858_command
    
        def GButton_858_command(self):
            print("command pressed to do anything")
            strMyVariable1 = self.GLineEdit_552.get()
            strMyVariable2 = self.GLineEdit_314.get()
            print(strMyVariable1)
            print(strMyVariable2)
    
    if __name__ == "__main__":
        root = tk.Tk()
        app = App(root)
        root.mainloop()
票数 0
EN

Stack Overflow用户

发布于 2022-08-21 15:20:41

另一种方法:创建属性来存储在文本框中获得的值:

代码语言:javascript
运行
复制
import tkinter as tk
import tkinter.font as tkFont

class App:
    def __init__(self, root):
        #setting title
        root.title("Login")
        #setting window size
        width=497
        height=214
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(alignstr)
        root.resizable(width=False, height=False)

        GLabel_129=tk.Label(root)
        GLabel_129["bg"] = "#ff8c00"
        ft = tkFont.Font(family='Times',size=28)
        GLabel_129["font"] = ft
        GLabel_129["fg"] = "#ffffff"
        GLabel_129["justify"] = "left"
        GLabel_129["text"] = "Login:"
        GLabel_129.place(x=0,y=10,width=497,height=66)

        GLabel_333=tk.Label(root)
        GLabel_333["bg"] = "#1e9fff"
        ft = tkFont.Font(family='Times',size=10)
        GLabel_333["font"] = ft
        GLabel_333["fg"] = "#01aaed"
        GLabel_333["justify"] = "center"
        GLabel_333["text"] = ""
        GLabel_333.place(x=0,y=80,width=497,height=15)

        self.GLineEdit_552=tk.Entry(root)   #To stay visible in other areas of the program the entry must be declared as self.
        self.GLineEdit_552["borderwidth"] = "1px" # All GLineEdit references must be self.
        ft = tkFont.Font(family='Times',size=14)
        self.GLineEdit_552["font"] = ft
        self.GLineEdit_552["fg"] = "#333333"
        self.GLineEdit_552["justify"] = "left"
        self.GLineEdit_552["text"] = ""
        self.GLineEdit_552.place(x=110,y=100,width=375,height=32)

        self.GLineEdit_314=tk.Entry(root) #To stay visible in other areas of the program the entry must be declared as self.
        self.GLineEdit_314["borderwidth"] = "1px" # All GLineEdit references must be self.
        ft = tkFont.Font(family='Times',size=14)
        self.GLineEdit_314["font"] = ft
        self.GLineEdit_314["fg"] = "#333333"
        self.GLineEdit_314["justify"] = "left"
        self.GLineEdit_314["text"] = ""
        self.GLineEdit_314.place(x=110,y=140,width=375,height=32)
        self.GLineEdit_314["show"] = "*"

        GLabel_702=tk.Label(root)
        ft = tkFont.Font(family='Times',size=14)
        GLabel_702["font"] = ft
        GLabel_702["fg"] = "#ff0000"
        GLabel_702["justify"] = "left"
        GLabel_702["text"] = "Username:"
        GLabel_702.place(x=10,y=100,width=100,height=32)

        GLabel_648=tk.Label(root)
        ft = tkFont.Font(family='Times',size=14)
        GLabel_648["font"] = ft
        GLabel_648["fg"] = "#ff0000"
        GLabel_648["justify"] = "left"
        GLabel_648["text"] = "Password:"
        GLabel_648.place(x=10,y=140,width=100,height=32)

        GButton_858=tk.Button(root) # a button to allow the creation of a method to process data, in the case of assigning the values typed to the properties created 
        GButton_858["bg"] = "#f0f0f0"
        ft = tkFont.Font(family='Times',size=10)
        GButton_858["font"] = ft
        GButton_858["fg"] = "#000000"
        GButton_858["justify"] = "center"
        GButton_858["text"] = "Button to do anything"
        GButton_858.place(x=200,y=180,width=120,height=25)
        GButton_858["command"] = self.GButton_858_command

        self.MyPropertyUsername = "" #Create property/variable for store entry GLineEdit_552.get()
        self.MyPropertyPassword = "" #Create property/variable for store entry GLineEdit_314.get()

    def GButton_858_command(self):
        print("command pressed to do anything")
        self.MyPropertyUsername = self.GLineEdit_552.get() #Get content of entry and store in property
        self.MyPropertyPassword = self.GLineEdit_314.get() #Get content of entry and store in another property
        root.destroy() #close window

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()

#Continue your program...;    
print("Continue your program...")
myEntryContent1 = app.MyPropertyUsername
myEntryContent2 = app.MyPropertyPassword
print("your first entry data is: " + myEntryContent1)
print("your second entry data is: " + myEntryContent2)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65810767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档