转自:用python写windowGUI程序 - 一张A4纸
一直以为windows上的UI程序只能用.net写,这让我这个web工程师很是遗憾,虽然之前学过python的爬虫,但是从来没往这方面想.最近突然发现可以用python写windows桌面程序,很是欣喜.
安装
写window程序当然要在window下运行,所以这里用的是anaconda
conda install tk
代码
这里给出一个最简单的桌面程序的例子
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'a hello world GUI example.'
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
self.nameInput = Entry(self)
self.nameInput.pack()
self.alertButton = Button(self, text='Hello', command=self.hello)
self.alertButton.pack()
def hello(self):
name = self.nameInput.get() or 'world'
messagebox.showinfo('Message', 'Hello, %s' % name)
app = Application()
app.master.title('Hello World')
# 主消息循环:
app.mainloop()
打包成exe
上面的程序还是.py结尾的,如何变成windows经常见到的.exe文件呢
pip install pyinstaller
pyinstaller -F -w -i icon.ico demo.py
生成的exe文件就在子目录dist中。-F表示指定打包后只生成一个exe格式的文件,-w表示窗口,无控制台,-i是图标
网友评论