使用python TK获取代理,用户名和密码

时间:2017-06-08 22:45:33

标签: python tkinter tk

我尝试使用带有以下代码的简单TK GUI获取用户的代理信息:

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

当我在外面调用该函数时,所有字符串字段都不会改变。

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

getProxyCredentials(proxy, username, password)

传入的变量仍然是空白的,即使它们是在函数中分配的。字符串通过引用传递,因此它们不应再为空白。我错过了什么/做得不好?

1 个答案:

答案 0 :(得分:0)

考虑使用tkinter.simpledialog实现一个类。您可以编写一些内容以将结果返回给程序的主体。

这是一个可以修改以添加代理字段的简单示例。

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)