首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python_学习之图形用户界面GUI

# date 2018-01-05

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

#!/usr/bin/python

图形用户界面GUI(graphical user interface)

搭建最简单的gui,Python中通过模块easygui.py来实现

安装easygui

2.无论windows、Linux、maxos将下载下来的easygui.py放在Python安装的主文件中 即可

3.打开Python导入模块

import easygui

easygui.msgbox("Hello There!")

一、一个按钮式对话框

# easygui.msgbox()函数用于创建一个对话框,一条信息和一个ok按钮的对话框,点击ok后关闭

用户点击了什么按钮,键入了什么内容,easygui会在用户操作完返回信息,这个叫响应,可为其指定一个变量

user_enter=easygui.msgbox("hello world!","title")

print(user_enter)

返回结果:ok

二、多个按钮式对话框

# easygui.buttonbox()函数用于创建多个按钮的对话框

语法:

# easygui.buttonbox("对话框提示信息",'对话框标题',('按钮一','按钮 二','按钮三','按钮四'))

或者

# easygui.buttonbox("对话框提示信息",标题='',choices=['按钮一','按钮 二'])

importeasygui

flavor=easygui.choicebox('what is your favorite ice cream flavor?','title',choices=('oen','two','three'))

easygui.msgbox("your choice"+flavor)

# 上例做了嵌套通过msgbox将用户输入的信息显示出

# 加号+ 是字符串连接符

三、列表式选择框

# easygui.choicebox()通过列表形式选择

eg:

import easygui

flavor = easygui.chiocebox("What is your favorite ice cream flavor?",

choices = ['one', ‘two', ‘three’] )

easygui.msgbox ("You choice " + flavor)

注:列表显示无法调整上面的对话框大小这是在easygui.py定义了如果想变需修改文件

修改方式如下:

1.查找easygui.py文件中def_choicebox

2.在其下找root_width=int((scree_width*0.8))

root_height=int((scree_height*0.5))

3.改变其值,保存即可

四、文本输入

# easygui.enterbox()允许用户输入文本作为程序选项

easygui.enterbox("信息","标题(可不要)")

easygui.enterbox('message','title',default='five')

五、默认输入(当用户不输入值是选择内置默认值,也可删掉它自己填)

import easygui

a=easygui.enterbox("what is your favorite ice?","title",default='草莓味')

easygui.msgbox("your choice" + a)

六、数字输入

整型easygui.integerbox()

浮点型没有,可以先输入字符串再通过float()函数进行转换

import easygui

number=easygui.enterbox("please enter your like number",'Title_number')

a=float(number) +3.2

easygui.msgbox( float(a) ,'title_test')

拓展1:

import random,easygui

secret = random.randint(1,99)

guess = 0

tries = 0

easygui.msgbox("""AHOY ! I'm the Dread Pirate Roberts,and i have secret!

It is a number from 1 to 99. I'll give you 6 tries.""")

while guess != secret and tries < 6:

guess = easygui.integerbox("what's yer guess?")

if not guess:break

if guess < secret:

easygui.msgbox(str(guess) + "Too low,ye scurvy dog!")

elif guess > secret:

easygui.msgbox(str(guess) + "Too high,landlubber!")

tries +=1

if guess == secret:

easygui.msgbox("Avast! ye got it! Found my secret,ye did!")

else:

easygui.msgbox("No more guesses! Better luck next time, matey!")

easygui.msgbox("no more guesses! The secret number was " + str(secret))

语法一定要对齐,不然报错!!!

拓展2:

import easygui

a=easygui.enterbox("enter your name:",'name_title',default='china')

b=easygui.enterbox("enter your room_number:",'room_number_title',default='1-45')

c=easygui.enterbox("enter your city:",'city_title',default='Beijing')

d=easygui.enterbox("enter your sd:",'街道_title',default='天安门广场')

e=easygui.enterbox("enter your a_number",'邮编_title',default='12345')

f=a+"\n"+b+"\n"+c+","+d+"\n"+e

easygui.msgbox(f,"here is your address:")

# f=str(a)+str(b)+str(c)+str(d)+str(e)--error 有ascii识别不了的字符

# f=a+b+c+d+e 各变量系统已经认为其是字符串类型

# easygui.msgbox(f) 这样会在一行显示

#\n 为换行符

回顾:

import easygui

easygui.msgbox() 单个按钮

easygui.buttonbox() 多个按钮

easygui.choicebox() 列表形式

easygui.enterbox() 文本框输入

easygui.integerbox() 整数输入(0-99)

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180110G037EW00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券