python 编写GUI程序

Python GUI库最常用三种:

1.tkInter

这是 Python 做的 tk/tcl 的壳。python官方标准库,支持多平台。但界面比较初级和基本,基本不商用。使用参考:https://blog.csdn.net/laizhenghong2012/article/details/78988500

布局:https://blog.csdn.net/qq_25600055/article/details/47061295

使用参考:http://www.runoob.com/python/python-gui-tkinter.html

2.wxPython

推荐,优秀GUI库,wxPython是作为优秀的跨平台GUIwxWidgetsPython封装和Python模块的方式提供给用户的。支持winunixlinux

安装:

Cmd下直接执行:pip install wxPython

一般不成功,到https://pypi.org/project/wxPython/#files进行下载,注意python版本号对应包。

安装参考:https://blog.csdn.net/xiongwanfeng/article/details/77995055

本地包安装:pip install wxPython-4.0.2-cp36-cp36m-win32.whl

注:whl格式本质上是一个压缩包,其中包含py文件,以及编译过的pyd文件

程序出现错误:AttributeError: module 'wx' has no attribute 'Frame',发现是文件名为wx,与模块重名了,改名就解决了。

安装过程:

D:\知识库\language\python\pack>pip install wxPython-4.0.2-cp36-cp36m-win32.whl

Processing d:\知识库\language\python\pack\wxpython-4.0.2-cp36-cp36m-win32.whl

Collecting PyPubSub (from wxPython==4.0.2)

  Downloading https://files.pythonhosted.org/packages/ab/9e/3b50915d3346971aaa49074425788598ee4907e67c097e013f1a862bd45c

/Pypubsub-4.0.0-py3-none-any.whl (63kB)

    100% |████████████████████████████████| 71kB 151kB/s

Collecting six (from wxPython==4.0.2)

  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a

/six-1.11.0-py2.py3-none-any.whl

Installing collected packages: PyPubSub, six, wxPython

  The scripts helpviewer.exe, img2png.exe, img2py.exe, img2xpm.exe, pycrust.exe, pyshell.exe, pyslices.exe, pyslicesshel

l.exe, pywxrc.exe, wxdemo.exe, wxdocs.exe and wxget.exe are installed in 'c:\program files\python36-32\Scripts' which is

 not on PATH.

  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Successfully installed PyPubSub-4.0.0 six-1.11.0 wxPython-4.0.2

 

3.PyQT

来自:https://blog.csdn.net/hongbochen1223/article/details/78588454

安装方法:

pip3 install pyqt5

D:\知识库\language\python\pack>pip3 install pyqt5

在线一般都失败,在这个网站下载:https://pypi.org/project/PyQt5/#files

下载包后安装:

D:\知识库\language\python\pack>pip install PyQt5-5.10.1-5.10.1-cp35.cp36.cp37.cp38-none-win32.whl

Processing d:\知识库\language\python\pack\pyqt5-5.10.1-5.10.1-cp35.cp36.cp37.cp38-none-win32.whl

Collecting sip<4.20,>=4.19.4 (from PyQt5==5.10.1)

  Downloading https://files.pythonhosted.org/packages/7a/49/67cc7955baf2ec5b67e141da2ab2a436cbf0f8d7c9fcab54e35df21d056b

/sip-4.19.8-cp36-none-win32.whl (42kB)

    100% |████████████████████████████████| 51kB 27kB/s

Installing collected packages: sip, PyQt5

  The scripts pylupdate5.exe, pyrcc5.exe and pyuic5.exe are installed in 'c:\program files\python36-32\Scripts' which is

 not on PATH.

  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Successfully installed PyQt5-5.10.1 sip-4.19.8

 

----------------------------------------例:tkinter示例----------------------------------

# -*- coding: UTF-8 -*-

from tkinter import *

root = Tk()                     # 创建窗口对象的背景色                                 

# 创建两个列表

li     = ['C','python','php','html','SQL','java']

movie  = ['CSS','jQuery','Bootstrap']

listb  = Listbox(root)          

#  创建两个列表组件

listb2 = Listbox(root)

for item in li:                 # 第一个小部件插入数据     

listb.insert(0,item)   

for item in movie:              # 第二个小部件插入数据     

listb2.insert(0,item)   

listb.pack()                    # 将小部件放置到主窗口中

listb2.pack()

root.mainloop()                 # 进入消息循环

----------------------------------------例:wxPython 示例----------------------------------

# -*- coding: UTF-8 -*-

import wx

class MainWindow(wx.Frame):

    def __init__(self, parent, title):

        wx.Frame.__init__(self, parent, title=title, size=(200,100))

        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)

        self.CreateStatusBar() # A Statusbar in the bottom of the window

        # Setting up the menu.

        filemenu= wx.Menu()

        # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.

        filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")

        filemenu.AppendSeparator()

        filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")

        # Creating the menubar.

        menuBar = wx.MenuBar()

        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar

        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

        self.Show(True)

app = wx.App(False)

frame = MainWindow(None, "Sample editor")

app.MainLoop()

----------------------------------------例:wxPython 示例----------------------------------

# -*- coding: UTF-8 -*-

import wx                               #导入wx

app = wx.App()                          #创建应用程序对象

win = wx.Frame(None,-1,'install test')  #创建窗体

btn = wx.Button(win, label = 'Button')  #创建Button

win.Show()                              #显示窗体

app.MainLoop()                          #运行程序

----------------------------------------例:pyqt示例----------------------------------

#-*- coding: utf-8 -*-

import sys

from PyQt5.QtWidgets import QApplication,QWidget

if __name__ == '__main__':

app = QApplication(sys.argv)

w = QWidget()

w.resize(250,150)

w.move(300,300)

w.setWindowTitle("Simple")

w.show()

sys.exit(app.exec_())

----------------------------------------例:pyqt示例2----------------------------------

# -*- coding: utf-8 -*-

import sys

from PyQt5.QtWidgets import (QWidget, QToolTip,

QPushButton, QApplication)

from PyQt5.QtGui import QFont

class Example(QWidget):

def __init__(self):

super().__init__()

self.initUI()

def initUI(self):

QToolTip.setFont(QFont("SanaSerif",10))

self.setToolTip("This is a <b>QWidget</b> widget")

btn = QPushButton('Button',self)

btn.setToolTip('This is a <b>QPushButton</b> widget')

btn.resize(btn.sizeHint())

btn.move(50,50)

self.setGeometry(300,300,300,200)

self.setWindowTitle("ToolTip")

self.show()

if __name__ == '__main__':

app =QApplication(sys.argv)

ex = Example()

sys.exit(app.exec_())

猜你喜欢

转载自blog.csdn.net/lyq_csdn/article/details/80820661