在Python中将按钮绑定到函数(Tkinter)

2024-05-13 19:38:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在才刚刚开始学习python,我想我可以尝试构建一个基本的计算器,这样我可以学习一些GUI概念,也可以学习后端概念。以下是我目前掌握的代码:

import tkinter
from tkinter import *
from tkinter import Button
import sys
import math

mainBox = Tk()
ment = StringVar()


mainBox.geometry("400x200")
mainBox.title("HandyCalc")

#Welcome screen buttons
welcomeLabel = Label(mainBox, text="Welcome to HandyCalc! Input your first 
number here: ")
welcomeEntry = Entry()
welcomeContinue = Button(mainBox, text = "Continue", command = continueFunc)

def continueFunc():
if welcomeEntry != int:
    intErr = Tk()


def welcome ():
print(welcomeLabel.pack())
print(welcomeEntry.pack())
print(welcomeContinue.pack())




def main():
welcome()

main()

当我运行这个程序时,窗口弹出,但是控制台返回一个错误“contineFunc is not defined”。我已经研究了很多类似问题的文章,但到目前为止还没有这样的运气。谢谢你的帮助。在


Tags: textfromimport概念tkinterdefbuttonpack
1条回答
网友
1楼 · 发布于 2024-05-13 19:38:03

下面的代码将button绑定到function(这print是演示的字符串),而且它的只是

import tkinter as tk


def function():
    print("This is invoked by a button tied to a function.")

root = tk.Tk()

button = tk.Button(root, text="My Button", command=function)
button.pack()

root.mainloop()

相关问题 更多 >