需要 Tkinter 帮助:尝试在按下按钮时将文本输入到条目中以在控制台中打印。我无法打印[重复]
Posted
技术标签:
【中文标题】需要 Tkinter 帮助:尝试在按下按钮时将文本输入到条目中以在控制台中打印。我无法打印[重复]【英文标题】:Need Tkinter Help: Trying to get text entered into entries to print in console when buttons are pressed. I cannot get it to print [duplicate] 【发布时间】:2021-08-18 07:44:30 【问题描述】:该项目的目标是让用户能够在 tkinter 窗口中输入 m 和 b 值,并在按下按钮时显示图表。有多个窗口。有一个点击开始按钮和一个选择图表类型的菜单。目前我只专注于让线性图工作。当到达线性窗口时,它会提示用户输入 m 和 b 值(通过条目)。
当按下图形按钮时,我已对其进行编码以打印 m 和 b 值。截至目前,我无法让它在控制台中打印出来。如果有人可以帮忙告诉我,谢谢!
代码:
from tkinter import *
root = Tk()
root.geometry("500x500")
class testInputs():
def __init__(self,m,b):
#self.displaygframe = Frame(root)
#self.displayglabe = Label(self.displaygframe,
#text="Test for now",
#font= "ComicSansMS 14")
print(m)
print(b)
class selectGraph(): #2
def __init__(self):
self.gselectframe = Frame(root)
self.gselectlabel = Label(self.gselectframe,
text = "Select Graph Type",
font= "ComicSansMS 14")
self.Var = IntVar()
#define buttons
self.linearButton = Radiobutton(self.gselectframe,
text="Linear",
variable = self.Var,
value=1)
self.quadraticButton = Radiobutton(self.gselectframe,text="Quadratic",
variable = self.Var,
value = 2)
self.confirmButton = Button(self.gselectframe, text = "Continue", command = self.transferAnswer,
fg = "red", font = "ComicSansMS 14",
bd = 2, bg = "light green")
self.sgBack = Button(self.gselectframe, text = "Back", command = self.backButton,
fg = "red", font = "ComicSansMS 5",
bd = 2, bg = "light green")
#pack
self.gselectlabel.pack()
self.linearButton.pack(anchor=W)
self.quadraticButton.pack(anchor=W)
self.confirmButton.pack()
self.gselectframe.pack()
mainloop()
def transferAnswer(self):
answer= self.Var.get()
print(str(answer))
self.gselectframe.destroy()
getEquation(answer)
def backButton(self):
self.gselectframe.destroy()
introWindow()
class getEquation(): #3
def __init__(self, answer):
if answer == 1:
self.linearInput()
if answer == 2:
self.quadraticInput()
else:
selectGraph()
def linearInput(self):
self.linearFrame = Frame(root)
#define widgets
self.linearLabel = Label(self.linearFrame,
text = "Enter Linear Equation",
font= "ComicSansMS 14")
self.linearBack = Button(self.linearFrame, text = "Back", command = self.backButtonLinear,
fg = "red", font = "ComicSansMS 5",
bd = 2, bg = "light green")
formatLabel = Label(self.linearFrame,
text="Format:",
font="ComicSansMS 14")
equationLabel = Label(self.linearFrame,
text="y=mx+b",
font="ComicSansMS 14",
fg="midnight blue")
#name = StringVar()
#name2 = StringVar()
self.mLabel = Label(self.linearFrame,
text= "m=",
font= "ComicSansMS 14")
self.mInput = Entry(self.linearFrame,
width =2, font="ComicSansMS 14")
self.bLabel = Label(self.linearFrame,
text = "b=",
font = "ComicSansMs 14")
self.bInput = Entry(self.linearFrame,
width=2,
font="ComicSansMS 14")
#get info from widget
m = self.mInput.get()
b = self.mInput.get()
self.graphButton = Button(self.linearFrame, text = "Graph", command = self.nextFromLinear(m,b), #error from here
fg = "red", font = "ComicSansMS 14",
bd = 2, bg = "light green")
#place widgets on screen
#self.linearFrame.pack()
self.linearLabel.grid(row=0,column=0)
formatLabel.grid(row=1,column=0)
equationLabel.grid(row=2, column=0)
self.bInput.grid(row=4, column=1)
self.bLabel.grid(row=4, column=0)
self.mInput.grid(row=3, column=1)
self.mLabel.grid(row=3,column= 0)
self.graphButton.grid(row=5, column=1) #error from here
self.linearBack.grid(row=5,column=0)
self.linearFrame.pack()
mainloop()
def quadraticInput(self):
self.quadraticFrame = Frame(root)
self.quadraticLabel = Label(self.quadraticFrame,
text = "Enter Quadratic Equation",
font= "ComicSansMS 14")
self.quadraticLabel.pack()
self.quadraticBack = Button(self.quadraticFrame, text = "Back", command = self.backButtonQuadratic,
fg = "red", font = "ComicSansMS 5",
bd = 2, bg = "light green")
self.quadraticBack.pack(anchor=SW)
self.quadraticFrame.pack()
mainloop()
def backButtonLinear(self):
self.linearFrame.destroy()
selectGraph()
def backButtonQuadratic(self):
self.quadraticFrame.destroy()
selectGraph()
def nextFromLinear(self,m,b):
#self.linearFrame.destroy()
#testInputs(m,b)
print(m)
print(b)
#Figuring out how to write singular back button function for all equation Frames:
#def backButton(self, frame1):
#frame1.destroy()
#selectGraph
class introWindow(): #1
def __init__(self):
self.introframe = Frame(root)
self.introlabel = Label(self.introframe,
text = "Graphing Program",font="ComicSansMS 14")
self.introbutton = Button(self.introframe, text = "Click to start", command = self.windowRemoval,
fg = "red", font = "ComicSansMS 14",
bd = 2, bg = "light green")
self.introlabel.pack()
self.introbutton.pack()
self.introframe.pack()
mainloop()
def windowRemoval(self):
self.introframe.destroy()
selectGraph()
introWindow()
【问题讨论】:
这里的代码太多了,你还没有说清楚哪一部分是问题所在。删除与问题无关的代码。command
参数必须是函数引用,而不是函数调用。
【参考方案1】:
Button()
的 command
参数必须是函数引用,而不是函数调用。您在创建按钮时调用该函数,而不是在单击它时调用。
使用lambda
创建匿名函数。而且它必须在调用时调用get()
,在创建按钮时不能调用它,否则你会得到输入的初始值,而不是用户输入的值。
self.graphButton = Button(self.linearFrame, text = "Graph",
command = lambda: self.nextFromLinear(self.mInput.get(),self.bInput.get()), #error from here
fg = "red", font = "ComicSansMS 14",
bd = 2, bg = "light green")
【讨论】:
以上是关于需要 Tkinter 帮助:尝试在按下按钮时将文本输入到条目中以在控制台中打印。我无法打印[重复]的主要内容,如果未能解决你的问题,请参考以下文章
按下按钮后可以更改菜单栏文本吗? (Python,tkinter)