我正在尝试使用Entry字段来获取手动输入,然后使用该数据.
我发现的所有消息都声称我应该使用该get()功能,但我还没有找到一个简单的工作迷你示例,我无法让它工作.
我希望有人可以告诉我我做错了什么.这是一个迷你文件:
from tkinter import *
master = Tk()
Label(master, text="Input: ").grid(row=0, sticky=W)
entry = Entry(master)
entry.grid(row=0, column=1)
content = entry.get()
print(content) # does not work
mainloop()
Run Code Online (Sandbox Code Playgroud)
这给了我一个Entry我可以输入的字段,但是一旦输入数据,我就无法对数据做任何事情.
我怀疑我的代码不起作用,因为最初entry是空的.但是,一旦输入数据,我该如何访问输入数据?
如果利润率具有特定值 (0.23),我正在尝试为用户提供计算其预计销售利润的可能性。用户应该能够输入任何值作为预计销售额:
from tkinter import *
root = Tk()
margin = 0.23
projectedSales = #value of entry
profit = margin * int(projectedSales)
#My function that is linked to the event of my button
def profit_calculator(event):
print(profit)
#the structure of the window
label_pan = Label(root, text="Projected annual sales:")
label_profit = Label(root, text="Projected profit")
label_result = Label(root, text=(profit), fg="red")
entry = Entry(root)
button_calc = Button(root, text= "Calculate", command=profit_calculator)
button_calc.bind("<Button-1>", profit_calculator)
#position of the elements on the window
label_pan.grid(row=0)
entry.grid(row=0, column=1)
button_calc.grid(row=1)
label_profit.grid(row=2) …Run Code Online (Sandbox Code Playgroud) 我想创建一个弹出消息框,提示用户输入输入.我在一个类中有这个方法.我将我的代码基于java2s的本指南.
class MyDialog:
def __init__(self, parent):
top = self.top = Toplevel(parent)
Label(top, text="Value").pack()
self.e = Entry(top)
self.e.pack(padx=5)
b = Button(top, text="OK", command=self.ok)
b.pack(pady=5)
def ok(self):
print "value is", self.e.get()
self.top.destroy()
root = Tk()
d = MyDialog(root)
root.wait_window(d.top)
Run Code Online (Sandbox Code Playgroud)
但在这方面,top = self.top = Toplevel(parent)对我不起作用.
我有一个我想要完成的模型.

我的程序结构看起来像这样:
class MainUI:
def__int__(self):
...
self.initUI()
def initUI(self):
.......
Popup = Button(self, text="Enter Value", command=self.showPopup)
def showPopup(self):
#create the popup with an Entry here
Run Code Online (Sandbox Code Playgroud)
如何在Python中创建一个接受用户输入的消息框?