简体   繁体   English

无法进入 tkinter,python

[英]Unable to get entry in tkinter, python

I'm new to coding and I'm trying to grab an input from an entry using tkinter in python.我是编码新手,我正在尝试使用 python 中的 tkinter 从条目中获取输入。 In theory, I should click the 'upload' button, then the code will get the entry and print it for me, but this isn't working.理论上,我应该单击“上传”按钮,然后代码将获取条目并为我打印,但这不起作用。 This is my code.这是我的代码。

from tkinter import *

root = Tk()
frame = Frame(root)
frame.pack()

def cancel():
    quit()

def upload():
    Entry.get()
    print(Entry)



bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

whitebutton = Entry(frame, fg="black")
whitebutton.pack( side = TOP)

redbutton = Button(frame, text="Cancel", fg="red", command = cancel)
redbutton.pack( side = LEFT)

bluebutton = Button(frame, text="Upload URL", fg="blue", command = upload)
bluebutton.pack( side = RIGHT )

root.mainloop()

Does anyone know what's going wrong here?有谁知道这里出了什么问题? Thanks, Kieran.谢谢,基兰。

Entry is a class in __init__ file in tkinter folder. Entry__init__文件夹中__init__文件中的一个类。

Instead of this:取而代之的是:

Entry.get()
print(Entry)

This is what you need这就是你需要的

var=whitebutton.get()
print(var)

First of all, it is better to use Tkinter variables rather than normal python variables.首先,最好使用 Tkinter 变量而不是普通的 python 变量。 Here you need to use StringVar() to set and get an user input from entry.在这里,您需要使用StringVar()来设置和获取来自条目的用户输入。 So the complete code -所以完整的代码 -

from tkinter import *

root = Tk()
var = StringVar()

frame = Frame(root)
frame.pack()

def cancel():
    quit()

def upload():
    print(var.get())



bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

whitebutton = Entry(frame,textvariable=var ,fg="black")
whitebutton.pack( side = TOP)

redbutton = Button(frame, text="Cancel", fg="red", command = cancel)
redbutton.pack( side = LEFT)

bluebutton = Button(frame, text="Upload URL", fg="blue", command = upload)
bluebutton.pack( side = RIGHT )

root.mainloop()

For more info - this and this欲了解更多信息 - 这个这个

put root var before root.mainloop()root var 放在 root.mainloop() 之前
and make app var and put into "Application(root)"并制作应用程序变量并放入“应用程序(root)”
then change ""root".mainloop()" to "app.mainlop()"然后将 ""root".mainloop()" 更改为 "app.mainlop()"

import tkinter as tk

// your code

root = tk.Tk()
app = Application(root)
app.mainloop()

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

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