简体   繁体   English

如何使用 tkinter 从用户输入中获取数据?

[英]How do I get data from user input with tkinter?

I am trying to have a have the user input a name to check against a list of names, however I am not sure how to store the user input to be used in the whitelist function.我试图让用户输入一个名称以检查名称列表,但是我不确定如何存储要在白名单 function 中使用的用户输入。 I tried using if e in whitelist: but it does not work, what should I put in my if clause?我尝试在白名单中使用 if e:但它不起作用,我应该在 if 子句中添加什么? Or is there another solution或者有没有其他解决方案

from tkinter import Message
from tkinter import *
import pandas as pd
from openpyxl import load_workbook

root = Tk()
root.geometry('400x200')

#Excel sheet being used
book = load_workbook('Desktop\python\excel.xlsx')
#Active sheet
sheet = book['names']

whitelist = set(row[0].value for row in sheet.rows)

#User input name to check for whitelist
e = Entry(root, width=20)
e.pack()


#Condition to check if user is whitelisted
def whitelist():
    if e in whitelist:
        print("User is whitelisted")
    else:
        print("The user is not whitelisted")

button = Button(root, text="Check", command=whitelist)
button.pack()

root.mainloop()

you can check for an input field when a button is pressed by using the following code:您可以使用以下代码在按下按钮时检查输入字段:

from tkinter import *
root = Tk()

def checker(input):
    if input == "Hello":
        print("Hello")
    else:
        print("Not Hello")


#Create window
root.title("Hello World")
root.geometry("200x100")
#Create an entry input and a button that checks for the input
button = Button(root, text="Click Me!", command=lambda: checker(entry.get()))
button.pack()
entry = Entry(root)
entry.pack()
#RUN LOOP
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM