Search code examples
pythontkinterpyautoguitkinter-entry

Tkinter, PyAutoGUI Python


He everyone! I have one problem and I hope you will help me with that. I am doing window with question and I want to get the text from Entry. When I am getting that (I am using 'EntryValue = EntryName.get()') it isn't giving error, but when I want to print it with PyAutoGUI.typewrite(EntryValue) it isn't print that because there isn't any value in my variable.

That is my code:

**from tkinter import *
import pyautogui as pg
import time
EntryThatAnswersToQuestionValue = ""
def FunctionThatCreatesWindowThatFindsProgramm():
    WindowThatFindsProgram = Toplevel(Root)
    WindowThatFindsProgram.iconbitmap(r'C:\Users\Murad\Desktop\My Factory\Programmes\J.A.R.V.I.S\Icon\QuestionIcon.ico')
    WindowThatFindsProgram.title("J.A.R.V.I.S. Has a question.")
    WindowThatFindsProgram.geometry('350x250')
    LabelThatExplainsWhatToDoInWindowThatFindsProgram = Label(WindowThatFindsProgram,text="Please, write your programm's name sir."
    ,fg='black',font='Arial 10 bold')
    LabelThatExplainsWhatToDoInWindowThatFindsProgram.pack()
    EntryThatAnswersToQuestion = Entry(WindowThatFindsProgram)
    EntryThatAnswersToQuestion.pack()
    EntryThatAnswersToQuestionValue = EntryThatAnswersToQuestion.get()
    OKButton = Button(WindowThatFindsProgram,text="     OK     ",fg='black',font='Arial 10 bold',command=FunctionThatFindsProgramm)
    OKButton.pack()
def FunctionThatFindsProgramm():
    pg.hotkey('winleft')
    time.sleep(1)
    pg.typewrite(EntryThatAnswersToQuestionValue,3)
    time.sleep(1)
    pg.hotkey('enter')**

Root = Tk()
Root.iconbitmap(r'C:\Users\Murad\Desktop\My Factory\Programmes\J.A.R.V.I.S\Icon\Icon.ico')
Root.title("J.A.R.V.I.S.")
Root.geometry('960x540')
Root['bg'] = 'black'

MainMenu = Frame(Root,bg='black')
MainMenu.pack()

JarvisTextGreeting = Label(MainMenu,text="Hello, sir. You can tell the below commands for execution.",bg='black'
,fg='blue',font='Arial 15 bold')
JarvisTextGreeting.pack()

ButtonFindingTheProgramm = Button(MainMenu,text="J.A.R.V.I.S. Please find programm that I will tell you on my PC"
,bg='blue',fg='black',font='Arial 10 bold',command=FunctionThatCreatesWindowThatFindsProgramm)
ButtonFindingTheProgramm.pack()

Root.mainloop()

Solution

  • You have to take the variable as global variable in the function:

    from tkinter import *
    import pyautogui as pg
    import time
    EntryThatAnswersToQuestionValue = ""
    def FunctionThatCreatesWindowThatFindsProgramm():
        global EntryThatAnswersToQuestionValue
        WindowThatFindsProgram = Toplevel(Root)
        WindowThatFindsProgram.iconbitmap(r'C:\Users\Murad\Desktop\My Factory\Programmes\J.A.R.V.I.S\Icon\QuestionIcon.ico')
        WindowThatFindsProgram.title("J.A.R.V.I.S. Has a question.")
        WindowThatFindsProgram.geometry('350x250')
        LabelThatExplainsWhatToDoInWindowThatFindsProgram = Label(WindowThatFindsProgram,text="Please, write your programm's name sir."
        ,fg='black',font='Arial 10 bold')
        LabelThatExplainsWhatToDoInWindowThatFindsProgram.pack()
        EntryThatAnswersToQuestion = Entry(WindowThatFindsProgram)
        EntryThatAnswersToQuestion.pack()
        EntryThatAnswersToQuestionValue = EntryThatAnswersToQuestion.get()
        OKButton = Button(WindowThatFindsProgram,text="     OK     ",fg='black',font='Arial 10 bold',command=FunctionThatFindsProgramm)
        OKButton.pack()
    def FunctionThatFindsProgramm():
        pg.hotkey('winleft')
        time.sleep(1)
        pg.typewrite(EntryThatAnswersToQuestionValue,3)
        time.sleep(1)
        pg.hotkey('enter')
    
    Root = Tk()
    Root.iconbitmap(r'C:\Users\Murad\Desktop\My Factory\Programmes\J.A.R.V.I.S\Icon\Icon.ico')
    Root.title("J.A.R.V.I.S.")
    Root.geometry('960x540')
    Root['bg'] = 'black'
    
    MainMenu = Frame(Root,bg='black')
    MainMenu.pack()
    
    JarvisTextGreeting = Label(MainMenu,text="Hello, sir. You can tell the below commands for execution.",bg='black'
    ,fg='blue',font='Arial 15 bold')
    JarvisTextGreeting.pack()
    
    ButtonFindingTheProgramm = Button(MainMenu,text="J.A.R.V.I.S. Please find programm that I will tell you on my PC"
    ,bg='blue',fg='black',font='Arial 10 bold',command=FunctionThatCreatesWindowThatFindsProgramm)
    ButtonFindingTheProgramm.pack()
    
    Root.mainloop()