我想使用Tkinter在python中制作一个基准应用程序

czfnxgou  于 2023-01-22  发布在  Python
关注(0)|答案(2)|浏览(130)

我想让它显示用户输入,但它没有显示。请发送帮助。我已经尝试了多个职位的代码,但没有运气。我能够使它显示“选定”选项,但无济于事。请帮助我

import tkinter as tk
from tkinter.ttk import *
app = tk.Tk()
# Functions and scripts
def OnSubmit():
    print(str(usrnme.get()))
    print(str(passw.get()))
    print(str(selected.get()))

app.title("Pymark: Landing Page")
app.geometry("600x600")
app.resizable(0, 0)
desc = """

PyMark is the first benchmark created using python's native GUI creator "Tkinter" and using hashes to rank performance.
The sofware is light and can run on older CPU's like i7-4510U (the processor I use) and does not require a lot of memory.
I am still open for constructive critisism and community feedback on my work as I am a beginner programmer.

IMPORTANT: PyMark can still experience bugs thus, pull requests are encouraged!

Please continue enjoying PyMark! 

"""

# Landing Page
lpframe = Frame(app)
mainheading = Label(lpframe, text="Welcome to PyMark!", font = ('Times New Roman', 20))
maindesc = Label(lpframe, text=f'{desc}', font = ('Times New Roman', 9))
lpframe.pack()
mainheading.pack(padx = 10, pady = 10)
maindesc.pack(padx = 10, pady = 10)
# Login Page
loginframe = Frame(app)
headerframe = Frame(loginframe, relief = 'groove')
loginheader = Label(headerframe, text='Login / Register: ', font = ('Times New Roman', 12))
usrnmelb = Label(loginframe, text = 'Username: ', font = ('Times New Roman', 9))
passlb = Label(loginframe, text = 'Password: ', font = ('Times New Roman', 9))
selected = tk.StringVar()
register = Radiobutton(loginframe, text='Register', value='reg', variable=selected)
login = Radiobutton(loginframe, text='Login', value='log', variable=selected)
submitbtn = Button(loginframe, text = 'Submit', command=OnSubmit)
usrnmeentr = Entry(loginframe)
passentr = Entry(loginframe)
loginframe.pack()
headerframe.pack(padx = 20, pady = 20)
loginheader.pack(padx = 20, pady = 20)
usrnmelb.pack()
usrnmeentr.pack()
passlb.pack()
passentr.pack()
register.pack()
login.pack()
submitbtn.pack(padx = 10, pady = 10)
usrnme = tk.StringVar()
passw = tk.StringVar()
usrnmeentr.focus()
app.mainloop()

我期望它返回用户名、密码和选定的选项,但它只告诉选定的选项,没有其他内容。

yruzcnhs

yruzcnhs1#

OnSubmit()函数没有获得正确的输入来打印。使用.get()方法获取entryfields内容。在您的示例中,OnSubmit()函数应如下所示:

def OnSubmit():
    print(str(usrnmeentr.get())) #usrnme.get() was not the name of the entry box
    print(str(passentr.get()))
    print(str(selected.get()))
s4n0splo

s4n0splo2#

您必须将您的Entry***链接到Stringvar(),而不是单选按钮

import tkinter as tk

app = tk.Tk()
app.geometry('300x300')

# Functions and scripts
def OnSubmit():
    print(var.get())

var = tk.StringVar()
entry = tk.Entry(app, textvariable=var)
entry.pack()

bt = tk.Button(app, text='Show', command=OnSubmit)
bt.pack()
app.mainloop()

相关问题