当前分类:python>>正文

Python GUI Development:打造与众不同的桌面应用程序

来源:互联网   更新时间:2023年7月14日  

Python 笔记

一、GUI框架


import tkinter as tk

win = tk.Tk()
win.title("Python GUI")

label = tk.Label(win, text="Hello World!")
label.pack()

button = tk.Button(win, text="Click Me")
button.pack()

win.mainloop()

import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QWidget()
window.setWindowTitle("Python GUI")

layout = QtWidgets.QVBoxLayout()

label = QtWidgets.QLabel("Hello World!")
layout.addWidget(label)

button = QtWidgets.QPushButton("Click Me")
layout.addWidget(button)

window.setLayout(layout)

window.show()

sys.exit(app.exec_())

二、GUI小部件


# Tkinter
import tkinter as tk

win = tk.Tk()

label = tk.Label(win, text="Hello World!")
label.pack()

win.mainloop()

# PyQt
import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QWidget()

label = QtWidgets.QLabel("Hello World!")
layout.addWidget(label)

window.setLayout(layout)

window.show()

sys.exit(app.exec_())

# Tkinter
import tkinter as tk

win = tk.Tk()

button = tk.Button(win, text="Click Me")
button.pack()

win.mainloop()

# PyQt
import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QWidget()

button = QtWidgets.QPushButton("Click Me")
layout.addWidget(button)

window.setLayout(layout)

window.show()

sys.exit(app.exec_())

三、小技巧


import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QWidget()
window.setWindowTitle("Python GUI")

layout = QtWidgets.QHBoxLayout()

label1 = QtWidgets.QLabel("Label 1")
layout.addWidget(label1)

label2 = QtWidgets.QLabel("Label 2")
layout.addWidget(label2)

window.setLayout(layout)

window.show()

sys.exit(app.exec_())

import sys
from PyQt5 import QtWidgets, QtGui

app = QtWidgets.QApplication(sys.argv)

window = QtWidgets.QWidget()
window.setWindowTitle("Python GUI")

icon = QtGui.QIcon("my_icon.png")
window.setWindowIcon(icon)

window.show()

sys.exit(app.exec_())

import sys
from PyQt5 import QtWidgets, QtGui

app = QtWidgets.QApplication(sys.argv)

result = QtWidgets.QMessageBox.question(None, "Question", "Do you like Python?", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)

if result == QtWidgets.QMessageBox.Yes:
    print("You like Python!")
else:
    print("You don't like Python!")

sys.exit(app.exec_())

结束语

本文固定链接:https://6yhj.com/leku-p-4811.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: 智能还款