
正文
python按钮绑定函数 python添加按钮
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用python的Tkinter中的按钮,绑定的事件如何更改全局变量的问题
from Tkinter import *
def changeStringvar():
a.set("Changed!")
print(a.get()) ## 在这print a.get()就能打印在控制台里面python按钮绑定函数了
root=Tk()
a=StringVar()
button=Button(root,text='Change',command=changeStringvar)
label=Label(root,textvariable=a)
button.pack()
label.pack()
print(a.get()) ## 这个时候打印出来python按钮绑定函数的还没变化呢
mainloop()
相关问答
Q1: python tkinter怎么监听不同时候鼠标悬停在按钮上实现不同效果?
可以使用 tkinter 中的 bind 方法来监听鼠标悬停在按钮上的事件,并实现不同的效果。
首先,需要在创建按钮时绑定事件,如下所示:
from tkinter import *
root = Tk()
def on_enter(e):
# 鼠标悬停在按钮上时执行的操作
btn.config(bg='green')
def on_leave(e):
# 鼠标离开按钮时执行的操作
btn.config(bg='white')
btn = Button(root, text="Button")
btn.bind("Enter", on_enter)
btn.bind("Leave", on_leave)
btn.pack()
root.mainloop()
上面的代码中,当鼠标悬停在按钮上时,会触发 Enter 事件,调用 on_enter 函数,改变按钮背景颜色为绿色; 当鼠标离开按钮时,会触发 Leave 事件,调用 on_leave 函数,改变按钮背景颜色为白色。
另外,可以使用更多的bind,如Button-1,ButtonRelease-1,Double-Button-1等,来监听不同的事件。
这样就可以通过监听不同时候鼠标悬停在按钮上实现不同效果了。
Q2: python tkinter, 通过lambda表达式传递参数到按钮的点击事件函数
给tkinter的按钮添加点击事件处理函数,可以在创建按钮时将定义好的函数赋值给它的command属性,即
如果在点击按钮执行函数时还要传递参数,就要用到lambda了,写成以下形式
写一个完整的小例子,来计算c = a + b
Q3: python怎么设置button按钮
生活中我们会遇到各种各样的登录界面,比如在登陆QQ时将账号和密码输入完备后,需要点击“登录”才能进入到自己的QQ页面。在Python中,这里的“登录”就是用Button组件制作的一个按钮。
导入tkinter模块
from tkinter import*
定义函数,用于在shell页面回答按钮上面的问题
def answer(): print("你看我像靓仔吗?")
创建根窗口
root=Tk()
创建Button组件
button=Button(root,text="你是靓仔吗",command=answer)#创建变量用于存放Button以及Button中的参数,root为根窗口,text为按钮上的文本内容,command=answer的作用是将按钮与函数绑定在一起
在根窗口中展示Button组件
button.pack()
让根窗口持续展示
root.mainloop()
完整代码
from tkinter import*def answer(): print("你看我像靓仔吗?")root=Tk()button=Button(root,text="你是靓仔吗",command=answer)button.pack()root.mainloop()
成果展示
使用Python中的Button组件制作按钮,就分享到这里!
Q4: python tkinter编程中如何把一个函数附加到Button上?,比如写计算器中的等于号
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Tkinter
class Window:
def __init__(self, root):
self.root = root
self.setbtn = Tkinter.Button(root, text='Set Text', command=self.Settxt)
# 创建一个按钮对象python按钮绑定函数,command= 这个地方就是当按钮按下去时触发python按钮绑定函数的函数
self.setbtn.place(x=120, y=15)
self.edit = Tkinter.Text(root)
self.edit.place(y=50)
self.edit.insert(Tkinter.END, "this is original text")
def Settxt(self):
self.edit.insert(Tkinter.END, '\nthis is inster text')
root = Tkinter.Tk()
window = Window(root)
root.minsize(600, 480)
root.mainloop()
编写了一个最简单的例子,看了你就应该明了。
Q5: pythonmain.exe上有按钮
水平有限python按钮绑定函数,欢迎建议和挑错
PyQt5中按钮是一个QpushButton,可以提供一个点击python按钮绑定函数的按钮来触发摸一个事件。
按钮可以显示图片或者文字。
创建按钮
我们在第一节的mainwindow上创建一个按钮,代码如下python按钮绑定函数:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.setWindowModality(QtCore.Qt.WindowModal)
mainWindow.resize(624, 511)
self.centralWidget = QtWidgets.QWidget(mainWindow)
self.centralWidget.setObjectName("centralWidget")
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(240, 240, 75, 23))
self.pushButton.setObjectName("pushButton")
mainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle('您好')
mainWindow.setWindowIcon(QIcon('logo.png'))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = Ui_mainWindow()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
image
这里只是单单创建一个按钮,按钮点击并没有任何作用。
同样按钮也可以进行各种设置。
image
除python按钮绑定函数了上图几种方法,再介绍另外几种方法。
(1)设置字体大小,加粗,字型
font = QtGui.QFont()
font.setFamily('微软雅黑')
font.setBold(True)
font.setPointSize(13)
font.setWeight(75)
self.pushButton.setFont(font)
这个方法是通用的。几乎PyQt其他组件也能用。
image
(2)设置图片
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon)
self.pushButton.setIconSize(QtCore.QSize(50, 80))
self.pushButton.setAutoRepeatDelay(200)
image
无图代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.setWindowModality(QtCore.Qt.WindowModal)
mainWindow.resize(624, 511)
self.centralWidget = QtWidgets.QWidget(mainWindow)
self.centralWidget.setObjectName("centralWidget")
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(240, 240,200, 53))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("一颗数据小白菜")
# self.pushButton.setFlat(True)
self.pushButton.setStyleSheet("background-color: rgb(164, 185, 255);"
"border-color: rgb(170, 150, 163);"
"font: 75 12pt \"Arial Narrow\";"
"color: rgb(126, 255, 46);")
mainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle('您好')
mainWindow.setWindowIcon(QIcon('logo.png'))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = Ui_mainWindow()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
image
有图代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.setWindowModality(QtCore.Qt.WindowModal)
mainWindow.resize(624, 511)
self.centralWidget = QtWidgets.QWidget(mainWindow)
self.centralWidget.setObjectName("centralWidget")
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(240, 240,200, 53))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("一颗数据小白菜")
# self.pushButton.setFlat(True)
self.pushButton.setStyleSheet("background-color: rgb(164, 185, 255);"
"border-color: rgb(170, 150, 163);"
"font: 75 12pt \"Arial Narrow\";"
"color: rgb(126, 255, 46);")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pushButton.setIcon(icon)
self.pushButton.setIconSize(QtCore.QSize(50, 80))
self.pushButton.setAutoRepeatDelay(200)
mainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle('您好')
mainWindow.setWindowIcon(QIcon('logo.png'))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = Ui_mainWindow()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
image
绑定按钮事件
我们给按钮绑定触发时间,用:
pushbutton.clicked.connect()
(1)退出事件
我们给按钮绑定退出事件:
self.pushButton.clicked.connect(mainWindow.close)
image
python按钮绑定函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python添加按钮、python按钮绑定函数的信息别忘了在本站进行查找喔。





