Skip to content Skip to sidebar Skip to footer

Replacing Input() Portions Of A Python Program In A Gui

I am pretty new to python and have been working with a program that was originally meant for the command line. As such, it uses the input() function quite a bit, especially in the

Solution 1:

The easiest way is to overwrite the input function. Here's a working example using tkinter:

import tkinter as tk

def input(prompt=''):
    win= tk.Tk()

    label= tk.Label(win, text=prompt)
    label.pack()

    userinput= tk.StringVar(win)
    entry= tk.Entry(win, textvariable=userinput)
    entry.pack()

    # pressing the button should stop the mainloop
    button= tk.Button(win, text="ok", command=win.quit)
    button.pack()

    # block execution until the user presses the OK button
    win.mainloop()

    # mainloop has ended. Read the value of the Entry, then destroy the GUI.
    userinput= userinput.get()
    win.destroy()

    return userinput

print(input('foobar'))

Post a Comment for "Replacing Input() Portions Of A Python Program In A Gui"