简体   繁体   中英

Tkinter get the text from entry boxes

I want to know how to get the text from a Tkinter entry box. I made the UI with this website: https://visualtk.com . But now I dont know how to obtain the entered text. I paste my script here:

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()

Is there any command or function to get the text? Or do I need to have a button to enter the text?

  1. Created all entry elements as self.GLineEdit_... .
  2. Added button to have an method to run comands.
  3. Accessed entry elements (previously declared as self. ) using .get() method.

rewrited code:

    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()

Another approach: create properties to store the values obtained in textboxes:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM