Python宝典第11-15章:GUI编写

书上一共介绍了5种方法:

  • PythonWin
  • tkinter
  • wxPython
  • PyGTK
  • PyQT
调研以后,决定学习tkinter和PyQT。以下两段介绍摘自link:

Tkinter

Tkinter 似乎是与tcl语言同时发展起来的一种界面库。tkinter是python的配备的标准gui库,也是opensource的产物。Tkinter可用于windows/linux/unix/macintosh操作系统,而且显示风格是本地化的。Tkinter用起来非常简单,python自带的 IDLE就是采用它写的。

除此外,tkinter的扩展集pmw和Tix功能上都要相对它强大,但tkinter却是最基本的。我认为,在用python 做gui开发,tkinter是最基本的知识,所以这个环节是必须要学习的。你或许在以后的开发中并不常用tkinter,但是一些小型的应用上面,他还是很有用的,而且开发速度也很快。

PyQT

Qt同样是一种开源的GUI库,Qt的类库大约在300多个,函数大约在5700多个。Qt同样适合于大型应用,由它自带的qt designer可以让我们轻松来构建界面元素。




使用tkinter编写GUI:
tkinter是Python自带的GUI编程模块,是对图形库TK的封装。跨平台。
import tkinter
首先要生成一个主窗口对象,然后才能使用tkinter模块的函数方法等。生成主窗口后,再添加组件,或者调用其mainloop方法进行消息循环。

<span style="font-size:14px;"><span style="font-size:14px;"># -*- coding:utf-8 -*-
# file: pyLearningtkinter.py
#
from tkiter import *
root=Tk()
root.mainloop</span></span>

使用相应组件函数生成组件,然后使用pack、grid、place等方法添加的窗口中。
如果添加组件的时候不指定位置,tkinter会自动安排到合适位置。
<span style="font-size:14px;"><span style="font-size:14px;"># -*- coding:utf-8 -*-
# file: pyLearningtkinter.py
#
from tkinter import *
root=Tk()
label=Label(root, text="hello, tkinter!")
label.pack()
button1=Button(root, text="button1")
button1.pack(side=LEFT)
button1=Button(root, text="button2")
button1.pack(side=RIGHT)
root.mainloop()</span></span>

一共有15个核心组件。
按钮:
<span style="font-size:14px;"># -*- coding:utf-8 -*-
# file: pyLearningtkinter.py
#
from tkinter import *
root=Tk()
button1=Button(root, anchor=E, text="button1", width=40, height=5)
button1.pack()
button2=Button(root, text="button2", bg="blue")
button2.pack()
button3=Button(root, text="button3", width=14, height=1)
button3.pack()
button4=Button(root, text="button4", width=60, height=5, state=DISABLED)
button4.pack()
root.mainloop()</span>

文本框:
<span style="font-size:14px;"># -*- coding:utf-8 -*-
# file: pyLearningtkinter.py
#
from tkinter import *
root=Tk()
entry1=Entry(root, show='*')
entry1.pack()
entry2=Entry(root, show='#', width=50)
entry2.pack()
entry3=Entry(root, bg='red', fg='blue')
entry3.pack()
entry4=Entry(root, selectbackground='red', selectforeground='gray')
entry4.pack()
entry5=Entry(root, state=DISABLED)
entry5.pack()
edit1=Text(root, selectbackground='red', selectforeground='gray')
edit1.pack()
root.mainloop()</span>

标签:Label
菜单:
<span style="font-size:14px;"># -*- coding:utf-8 -*-
# file: pyLearningtkinter.py
#

from tkinter import *
root=Tk()
menu=Menu(root)
submenu=Menu(menu, tearoff=0)
submenu.add_command(label="Open")
submenu.add_command(label="Save")
submenu.add_command(label="Close")
menu.add_cascade(label="File", menu=submenu)
submenu=Menu(menu, tearoff=0)
submenu.add_command(label="Copy")
submenu.add_command(label="Paste")
submenu.add_separator()
submenu.add_command(label="Cut")
def popupmenu(event):
    menu.post(event.x_root, event.y_root)
root.bind("<Button-3>", popupmenu)
menu.add_cascade(label="Edit", menu=submenu)
submenu=Menu(menu, tearoff=0)
submenu.add_command(label="About")
menu.add_cascade(label="Help", menu=submenu)
root.config(menu=menu)
root.mainloop()</span>

单选框和复选框:
关键参数variable,其指定的变量应使用tkinter.IntVar或者tkinter.StringVar生成。可以用set方法设置初始值。如果初始值与组件value值一样,则该组件处于被选中状态。
若用了参数indicatoron传递值为0时,组件为按钮的样式。

绘制图形:Canvas

事件处理:
用组件的bind方法或者bind_class方法
bind(sequence, func, add)
bind_class(className, sequence, func, add)
bind_all(sequence, func, add)

对话框:
消息框需要使用tkinter的messagebox模块
另有simpledialog模块,filedialog模块,colorchooser模块

Toplevel组件来创建自定义对话框



使用PyQT编写GUI
编写GUI脚本,应首先创建一个QtGui.QApplication对象,并向其传递命令行参数。
脚本的最后调用QtGui.QApplication对象的exec_方法进入消息循环。
可以使用QMainWindow的setCentralWidget方法添加组件到窗口。
<span style="font-size:14px;"># -*- coding:utf-8 -*-
# file: pyLearningPyQT.py
#

import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('PyQT')
        self.resize(300,200)
        label=QtGui.QLabel("python\nlabel")
        label.setAlignment(QtCore.Qt.AlignCenter)
        self.setCentralWidget(label)
app=QtGui.QApplication(sys.argv)
mywindow=MyWindow()
mywindow.show()
app.exec_()
</span>

但是setCentralWidget只能添加一个组件。添加多个组件,需要使用布局组件来组织这些组件。
布局组件可以容纳多个组件,还可以设置组件位置。
常用布局组件:
  • QLayout
  • QHBoxLayout
  • QVBoxLayout
  • QGridLayout
共有的方法:
  • addWidget()
  • addLayout()
PyQt中的空白项可以占据位置,这样可以更好的控制其他组件位置。
使用QtGui.QSpacerItem创建空白项,创建时可以使用宽度和高度设置大小。
创建后,使用布局组件的additem方法将空白项添加的布局组件中。

按钮:
使用PyQt的QtGui.QPushButton,按钮事件是通过信号/插槽的形式将按钮事件绑定到类的方法上。
QT的组件是使用信号和插槽的形式来进行通信的。Qt的组件有很多预定义的信号,当事件激发时,信号被发送到插槽进行处理。
插槽实际上是处理特点信号的函数。
PyQt也是如此,使用组件的connect方法将组件的信号绑定到其处理插槽上。
connect (QObject, SIGNAL(), SLOT(). Qt.ConnectionType)
# -*- coding:utf-8 -*-
# file: pyLearningPyQt.py
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('PyQt')
        self.resize(300,200)
        gridlayout=QtGui.QGridLayout()
        self.button1=QtGui.QPushButton('Button1')
        gridlayout.addWidget(self.button1, 1,1,1,3)
        self.button2=QtGui.QPushButton('Button2')
        self.button2.setFlat(True)
        gridlayout.addWidget(self.button2,2,2)
        self.setLayout(gridlayout)
        self.connect(self.button1, QtCore.SIGNAL('clicked()'),self.OnButton1)
        self.connect(self.button2, QtCore.SIGNAL('clicked()'),self.OnButton2)
    def OnButton1(self):
        self.button1.setText('clicked')
    def OnButton2(self):
        self.button2.setText('clicked')
app=QtGui.QApplication(sys.argv)
mywindow=MyWindow()
mywindow.show()
app.exec_()

文本框,单选框,复选框类似按钮的创建。
菜单:
使用QMenuBar创建菜单条,QMenu创建菜单。addMenu方法向菜单条添加菜单。
addAction方法向菜单中添加菜单命令。对于QtGui.QMainWindow可以直接使用其menuBar方法获得菜单条。

Qt中,资源文件是以ui为后缀名的文件。Qt提供了Qt Designer用于创建资源文件。
该资源文件可以在PyQt中使用,简化界面设计,也可以将界面和代码分离,提供程序的可维护性。
# -*- coding:utf-8 -*-
# file: pyLearningPyQt.py
#
import sys
from PyQt4 import QtCore, QtGui, uic
class MyDialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        uic.loadUi("F:\\rantom\\python3.4\\Lib\\site-packages\\PyQt4\\res.ui", self)
class MyWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('PyQt')
        self.resize(300,200)
        gridlayout=QtGui.QGridLayout()
        self.button=QtGui.QPushButton('CreateDialog')
        gridlayout.addWidget(self.button, 1,1)        
        self.setLayout(gridlayout)
        self.connect(self.button, QtCore.SIGNAL('clicked()'),self.OnButton)        
    def OnButton(self):
        dialog=MyDialog()
        r=dialog.exec_()
        if r:
            self.button.setText(dialog.lineEdit.text())
app=QtGui.QApplication(sys.argv)
mywindow=MyWindow()
mywindow.show()
app.exec_()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值