有没有一种方法可以在tkinter中使用按钮打印条目

时间:2020-06-22 23:35:44

标签: python tkinter

您好,我是tkinter和python的新手,所以我想知道是否有什么办法可以让我使用“添加”按钮在“历史记录”下方打印“名称”,“电子邮件”和“密码”条目标签。目的是帮助您记住您的帐户。虽然我不需要关闭时保留此数据的功能。 任何帮助将非常感激, 谢谢。 这是我的代码:

from tkinter import *
import tkinter as tk

root = Tk()
root.title("Login")
username = "Sam" 
password = "Sam"
#username entry
username_entry = Entry(root, borderwidth=10)

#password entry
password_entry = Entry(root, show='*', borderwidth=10)

username_entry.grid(row=1,column=2)
password_entry.grid(row=2,column=2)

myLabel1 = Label(root,text="Username")
myLabel2 = Label(root,text="Password")

myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)

def trylogin(): 
    if username == username_entry.get() and password == password_entry.get():
        print("Correct")
        createNewWindow()
    else:
        print("Wrong")

def createNewWindow():
    newWindow = tk.Toplevel(root)
    newWindow.title("Accounts")
    #ENTRY'S
    e1 = Entry(newWindow, width=50)
    e2 = Entry(newWindow, width=50)
    e3 = Entry(newWindow, width=50)

    e1.grid(row=1, column=2)
    e2.grid(row=2, column=2)
    e3.grid(row=3, column=2)

    #LABELS
    myLabel1 = Label(newWindow, text="Name")
    myLabel2 = Label(newWindow, text="EMAIL:")
    myLabel3 = Label(newWindow, text="PASSWORD:")
    myLabel4 = Label(newWindow, text="History:")

    myLabel1.grid(row=1, column=0)
    myLabel2.grid(row=2, column=0)
    myLabel3.grid(row=3, column=0)
    myLabel4.grid(row=4, column=1)

def addToWindow():
    myLabel5 = Label(newWindow, text="Name: " + e1.get())
    myLabel6 = Label(newWindow, text="Email: " + e2.get())
    myLabel7 = Label(newWindow, text="Password: " + e3.get())
    
    myLabel5.grid(row=7, column=1)
    myLabel6.grid(row=8, column=1)
    myLabel7.grid(row=9, column=1)


button = Button(newWindow, text="Add", command = addToWindow) 
button.grid(row=4, column=2)


button = Button(root, text="check", command = trylogin) 
button.grid(row=3, column=2)


root.mainloop()
app.mainloop()

2 个答案:

答案 0 :(得分:0)

我在createNewWindow函数中添加了一个函数,以将输入字段的值写入历史记录标题下方。这可能更漂亮,但是您列出了值。

下面的代码。将其放在脚本的第53行,然后在Button函数中为添加按钮调用命令。

from tkinter import *
import tkinter as tk

root = Tk()
root.title("Login")
username = "username" 
password = "password"
#username entry
username_entry = Entry(root, borderwidth=10)

#password entry
password_entry = Entry(root, show='*', borderwidth=10)

username_entry.grid(row=1,column=2)
password_entry.grid(row=2,column=2)

myLabel1 = Label(root,text="Username")
myLabel2 = Label(root,text="Password")

myLabel1.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)

def trylogin(): 
    if username == username_entry.get() and password == password_entry.get():
        print("Correct")
        createNewWindow()
    else:
        print("Wrong")

def createNewWindow():
    newWindow = tk.Toplevel(root)
    newWindow.title("Accounts")
    #ENTRY'S
    e1 = Entry(newWindow, width=50)
    e2 = Entry(newWindow, width=50)
    e3 = Entry(newWindow, width=50)

    e1.grid(row=1, column=2)
    e2.grid(row=2, column=2)
    e3.grid(row=3, column=2)

    #LABELS
    myLabel1 = Label(newWindow, text="Name")
    myLabel2 = Label(newWindow, text="EMAIL:")
    myLabel3 = Label(newWindow, text="PASSWORD:")
    myLabel4 = Label(newWindow, text="History:")

    myLabel1.grid(row=1, column=0)
    myLabel2.grid(row=2, column=0)
    myLabel3.grid(row=3, column=0)
    myLabel4.grid(row=5, column=1)
    
    def addToWindow():
        myLabel5 = Label(newWindow, text="Name: " + e1.get())
        myLabel6 = Label(newWindow, text="Email: " + e2.get())
        myLabel7 = Label(newWindow, text="Password: " + e3.get())
        
        myLabel5.grid(row=7, column=1)
        myLabel6.grid(row=8, column=1)
        myLabel7.grid(row=9, column=1)

    button = Button(newWindow, text="Add", command = addToWindow) 
    button.grid(row=4, column=2)

button = Button(root, text="check", command = trylogin) 
button.grid(row=3, column=2)

root.mainloop()
#app.mainloop()

答案 1 :(得分:0)

针对您的要求的次要修复程序,将其移至Newwindow函数内

void f1_1(int)