0

I'm trying to get a user's proxy information using a simple TK GUI with the following code:

def getProxyCredentials(proxy, username, password):
    root = Tk()

    #define the entry boxes for username password and proxy
    proxyBox = Entry(root)
    proxyBox.insert(0, 'some.default.proxy:8080')
    userBox = Entry(root)
    pwdbox = Entry(root, show = '*')
    Label(root, text = 'Proxy Server').pack(side = 'top')
    proxyBox.pack(side = 'top')
    Label(root, text = 'Username').pack(side = 'top')
    userBox.pack(side = 'top')
    Label(root, text = 'Password').pack(side = 'top')
    pwdbox.pack(side = 'top')

    def onokclick():
        proxy = proxyBox.get()
        username = userBox.get()
        password = pwdbox.get()
        root.destroy()
        return None

    Button(root, command=onokclick, text = 'OK').pack(side = 'top')
    root.mainloop()
    return True

When I call the function outside, all the string fields do not change.

proxy = ''
username = ''
password = ''

getProxyCredentials(proxy, username, password)

The passed in variables are still blank even though they are assigned within the function. Strings are passed by reference, so they should no longer be blank. What am I missing/not doing correctly?

1 Answers1

0

Consider implementing a class using tkinter.simpledialog. You can write something to return the result to the main body of your program.

Here is a simple example that could be modified to add the proxy field.

import tkinter as tk
from tkinter import simpledialog

class GetUserNamePassword(simpledialog.Dialog):

    def body(self, master):

        tk.Label(master, text="Username:").grid(row=0)
        tk.Label(master, text="Password:").grid(row=1)

        self.e1 = tk.Entry(master)
        self.e2 = tk.Entry(master, show='*')

        self.e1.grid(row=0, column=1)
        self.e2.grid(row=1, column=1)
        return self.e1 # initial focus

    def apply(self):
        first = str(self.e1.get())
        second = str(self.e2.get())
        self.result = (first, second) # or something

root = tk.Tk()
root.withdraw()
d = GetUserNamePassword(root)
print(d.result)
scotty3785
  • 6,763
  • 1
  • 25
  • 35