導航:首頁 > 編程語言 > Python設置按鈕

Python設置按鈕

發布時間:2022-12-19 03:55:30

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組件製作按鈕,就分享到這里!

② python腳本如何添加啟動和停止按鈕

用tkinter的button組件。
設定好字體大小size(int類型),在循環內部(以while舉例)加組件:

xunhuan=1 # 控制循環的開始與結束
# 定義開始循環
def start():
global xunhuan

xunhuan = 1

# 結束
def end():
global xunhuan

xunhuan = 0

size=(字的大小)
# 現在導庫
inport tkinter as tk # 輸入方便
window = tk.Tk()
s = tk.Button(window, text = "開始" , command = start) # 開始按鈕
e = tk.Button(window , text = "停止" , command = end) # 結束按鈕
# 繪制按鈕
s.pack()
e.pack()
# 下面是循環
while True:
if xunhuan:

...(循環內部要做的事)

window.mainloop() # 在tkinter中,這行代碼一定要加

③ pythondocx添加按鈕

在WORD中通過滑鼠右鍵菜單「插入」按鈕中的功能可以實現表格行列的添加,也可以通過「刪除單元格」按鈕中的刪除單元格的功能實現表格行列的刪除;當然還可以通過「布局」菜單中的刪除和添加功能實現。

④ pythonmain.exe上有按鈕

水平有限,歡迎建議和挑錯

PyQt5中按鈕是一個QpushButton,可以提供一個點擊的按鈕來觸發摸一個事件。

按鈕可以顯示圖片或者文字。

創建按鈕

我們在第一節的mainwindow上創建一個按鈕,代碼如下:

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

除了上圖幾種方法,再介紹另外幾種方法。

(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

⑤ 9、Python虛擬環境

(1)安裝虛擬環境
1.安裝virtualenv, 控制台使用命令:pip install virtualenv
2.創建一個以後存放虛擬環境的目錄如d:\envs, 可在控制台使用命令d: 回車,mkdir envs
3.創建虛擬環境,控制台輸入命令 :virtualenv 文件夾
4.進入虛擬環境cd 虛擬環境的Scripts, 然後使用命令activate,虛擬環境的名稱被小括弧括起來,並且出現在最前面說明成功進入虛擬環境
5.進入虛擬環境以後可以安裝第三方包(庫),使用命令 pip install ..., 出現successfully則表示安裝成功
6.如何驗證安裝的程序是否成功:
-》python 命令進入python shell環境檢測
-》import 包(架)名
沒有報錯,導入包成功,則表明安裝成功
7.退出虛擬環境 deactivate.bat, 最前面有小括弧的虛擬環境就沒有了
如果當時在python shell環境,則需要現在用quit()/exit()退出

(2)虛擬環境切換 Virtualenvwrapper
1.安裝virtualenvwrapper,使用命令 pip install virtualenvwrapper-win
2.配置管理虛擬環境的位置,系統環境變數
變數名: WORKON_HOME
變數值:虛擬環境路徑
3.虛擬環境自由切換:workon vname
註:3.7以上python目前不兼容。

(3)pychaml中導入虛擬環境
1.打開Pycharm,然後依次點擊左上方的file->Settings

2.之後依次Project->Project Interpreter

3.點擊右上方的設置按鈕,然後點擊Add

4.在彈出的Add Python Interpreter頁面中,依次選擇Virtualenv Environment(虛擬環境)->Existing environment

5.之後在彈出的界面中載入本地的虛擬環境,點擊右側的三個點號的按鈕

6.根據目錄,依次找到自己創建的虛擬環境路徑,我的路徑是I:->Virtual_environment_list->Scripts->python.exe,點擊OK按鈕

可以用於解決需要使用不同版本python的開發問題。

3.創建python(必須是本機已安裝版本) :

4.進入虛擬環境:

5.在虛擬環境中安裝模塊:

6.退出pipenv的虛擬環境:

7.查看我所在的位置:

8.顯示所在的位置:

9.查看一個目錄依賴於哪些目錄:

10.查看可用的程序:

11.刪除虛擬環境中所有已安裝模塊,且不編輯Pipfile.lock,刪除完用pipenv install能重新安裝:

12.刪除一個已安裝模塊,且從Pipfile.lock中刪除:

13.刪除虛擬環境:

閱讀全文

與Python設置按鈕相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:963
phpffmpeg轉碼 瀏覽:672
長沙好玩的解壓項目 瀏覽:145
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:486
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:382
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:350
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:151
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163