如何让Tkinter输入文本并使用按钮提交

时间:2010-12-27 17:45:03

标签: login tkinter

您好我想知道是否有人可以帮助我将文本字段中的代码提交到登录字段,然后使用登录按钮提交br.submit(name =“wp-submit”)

from Tkinter import *
import tkMessageBox
if ( __name__ == "__main__" ):
        import resources.lib.mechanize as mechanize
        mechanize

# Start Browser
br = mechanize.Browser(factory=mechanize.RobustFactory())

# User-Agent (Firefox)
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6')]
br.open('http://razetheworld.com/wp-login.php?redirect_to=http%3A%2F%2Frazetheworld.com')

br.select_form(name="loginform")
br['log'] = 'entryWidget_U must enter here'
br['pwd'] = 'entryWidget_P must enter here'
br.submit(name="wp-submit")

print br.geturl()

def displayText():
    """ Display the Entry text value. """

    global entryWidget_U
    global entryWidget_P

    if entryWidget_U.get().strip() == "":
        tkMessageBox.showerror("Tkinter Entry Widget", "Enter a Username")
    else:
        tkMessageBox.showinfo("Tkinter Entry Widget", "Text value =" + entryWidget_U.get().strip())

    if entryWidget_P.get().strip() == "":
        tkMessageBox.showerror("Tkinter Entry Widget", "Enter a Password")
    else:
        tkMessageBox.showinfo("Tkinter Entry Widget", "Text value =" + entryWidget_P.get().strip()) 

if __name__ == "__main__":

    root = Tk()

    root.title("Tkinter Entry Widget")
    root["padx"] = 40
    root["pady"] = 20       

    # Create a text frame to hold the text Label and the Entry widget
    textFrame_U = Frame(root)
    textFrame_P = Frame(root)

    #Create a Label in textFrame
    entryLabel = Label(textFrame_U)
    entryLabel["text"] = "Enter Username:"
    entryLabel.pack(side=LEFT)

    entryLabel = Label(textFrame_P)
    entryLabel["text"] = "Enter Password:"
    entryLabel.pack(side=LEFT)

    # Create an Entry Widget in textFrame
    entryWidget_U = Entry(textFrame_U)
    entryWidget_U["width"] = 50
    entryWidget_U.pack(side=LEFT)

    entryWidget_P = Entry(textFrame_P)
    entryWidget_P["width"] = 50
    entryWidget_P.pack(side=LEFT)

    textFrame_U.pack()
    textFrame_P.pack()

    button = Button(root, text="Login", command=#Run br.submit(name="wp-submit"))
    button.pack() 

    root.mainloop()

1 个答案:

答案 0 :(得分:2)

Nervermind我想通了

# File name: tkinterEntryWidget.py
# Author: S.Prasanna

from Tkinter import *
import tkMessageBox
if ( __name__ == "__main__" ):
        import resources.lib.mechanize as mechanize
        mechanize

def displayText():
    """ Display the Entry text value. """

    global entryWidget_U
    global entryWidget_P

    # Start Browser
    br = mechanize.Browser(factory=mechanize.RobustFactory())

    # User-Agent (Firefox)
    br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6')]
    br.open('http://razetheworld.com/wp-login.php?redirect_to=http%3A%2F%2Frazetheworld.com')

    br.select_form(name="loginform")
    br['log'] = entryWidget_U.get().strip()
    br['pwd'] = entryWidget_P.get().strip()
    submit = br.submit(name="wp-submit")

    print br.geturl()

    if entryWidget_U.get().strip() == "":
        tkMessageBox.showerror("Tkinter Entry Widget", "Enter a Username")
    else:
        tkMessageBox.showinfo("Tkinter Entry Widget", "Text value =" + entryWidget_U.get().strip())

    if entryWidget_P.get().strip() == "":
        tkMessageBox.showerror("Tkinter Entry Widget", "Enter a Password")
    else:
        tkMessageBox.showinfo("Tkinter Entry Widget", "Text value =" +
entryWidget_P.get().strip()) 

    if __name__ == "__main__":

    root = Tk()

    root.title("Tkinter Entry Widget")
    root["padx"] = 40
    root["pady"] = 20       

    # Create a text frame to hold the text Label and the Entry widget
    textFrame_U = Frame(root)
    textFrame_P = Frame(root)

    #Create a Label in textFrame
    entryLabel = Label(textFrame_U)
    entryLabel["text"] = "Enter Username:"
    entryLabel.pack(side=LEFT)

    entryLabel = Label(textFrame_P)
    entryLabel["text"] = "Enter Password:"
    entryLabel.pack(side=LEFT)

    # Create an Entry Widget in textFrame
    entryWidget_U = Entry(textFrame_U)
    entryWidget_U["width"] = 50
    entryWidget_U.pack(side=LEFT)

    entryWidget_P = Entry(textFrame_P)
    entryWidget_P["width"] = 50
    entryWidget_P.pack(side=LEFT)

    textFrame_U.pack()
    textFrame_P.pack()

    button = Button(root, text="Login", command=displayText)
    button.pack() 

    root.mainloop()