博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于tkinter的gui设计实例
阅读量:4170 次
发布时间:2019-05-26

本文共 3389 字,大约阅读时间需要 11 分钟。

基于tkinter的gui设计实例(野獸发言生成器v1.0.0)

以野獸先軰发言生成器为例

主事件循环

与其他gui窗口实现相似,其基础是一个循环刷新的主窗口,再tkinter库内,它继承于Tk()类。代码如下:

from tkinter import *from tkinter import messageboximport article_maker //引入核心算法root=Tk()//主窗口继承root.title('inm article creator')//title属性规定窗口标题root.geometry('500x300')//geometry属性设置窗口大小root.mainloop()//主事件循环

Label(标签)控件

标签控件,即为一段文字,没有任何交互,最为简单,先上代码

l1=Label(text="主题:")l2=Label(text="字数:")l1.place(x=0,y=20,anchor='nw')l2.place(x=0,y=40,anchor='nw')

如代码所示,标签控件继承于Label类,属性text为标签显示的文字,place为标签位置,anchor为定位锚点。具体定位方法可查阅相关资料。

Entry(输入框)控件

用于向程序中输入字符串

ent1=Entry(root,width=20)ent1.place(x=40,y=20,anchor='nw')ent2=Entry(root,width=20)ent2.place(x=40,y=40,anchor='nw')

注意,entry类的构造与place方法不能同时使用,会有副作用,参见:

entry类还有get()方法,用于获取字符串,用法见Button控件。

Canvas(画布)控件

canvas用于创建一个空白区域,以显示图形或文本,用法参见Button控件

Button(按钮)控件

按钮控件涉及控件的位置,显示文本,显示颜色,点击后颜色,点击后方法调用等,先上按钮本体的代码:

submit_button=Button(root,activebackground="#F83030",command=submit_handler,fg="#000000",text="submit!").place(x=50,y=240,anchor='nw')again_button=Button(root,activebackground="red",command=again_handler,fg="black",text="try again!").place(x=50,y=270,anchor='nw')

button构造函数中,root指明它所属的主窗口,activebackground指明它被点击时显示的颜色,command是点击按钮调用的方法,text是显示的文本,place()方法确定位置。

下面为调用的方法

def submit_handler():   title=ent1.get()   num=ent2.get()                                  //get()方法从entry中获得字符串   if(num.isdigit()):      article_maker.main(["lal",num,title])      messagebox.showinfo("提示","文章成功生成")   //messagebox显示消息框      tmp=""      curfile=open("output.txt")      curlines=curfile.readlines()      if len(curlines)<10:         for line in curlines:            tmp=tmp+'\n'+line      else:         for i in range(10):            tmp=tmp+'\n'+curlines[i]         //读取生成文章的前十行,并显示在画布上      curfile.close()      cv=Canvas(root,bg='white')      cv.place(x=120,y=20,anchor='nw')      cv.create_text(200,100,text=tmp,font="黑体,16",fill="Black")     else:        ent2.select_clear()        messagebox.showwarning("警告","字数需要为合法数字!")def again_handler():   ent1.select_clear()   ent2.select_clear()

其中 again_handler有点bug,点击之后不能实现entry清空,不知有没有带哥指点一下。

整体文件

from tkinter import *from tkinter import messageboximport article_makerroot=Tk()root.title('inm article creator')root.geometry('500x300')ent1=Entry(root,width=20)ent1.place(x=40,y=20,anchor='nw')ent2=Entry(root,width=20)ent2.place(x=40,y=40,anchor='nw')l1=Label(text="主题:")l2=Label(text="字数:")l1.place(x=0,y=20,anchor='nw')l2.place(x=0,y=40,anchor='nw')def submit_handler(): title=ent1.get() num=ent2.get() if(num.isdigit()):  article_maker.main(["lal",num,title])  messagebox.showinfo("提示","文章成功生成")  tmp=""  curfile=open("output.txt")  curlines=curfile.readlines()  if len(curlines)<10:   for line in curlines:    tmp=tmp+'\n'+line  else:   for i in range(10):    tmp=tmp+'\n'+curlines[i]  curfile.close()  cv=Canvas(root,bg='white')  cv.place(x=120,y=20,anchor='nw')  cv.create_text(200,100,text=tmp,font="黑体,16",fill="Black") else:  ent2.select_clear()  messagebox.showwarning("警告","字数需要为合法数字!")def again_handler(): ent1.select_clear() ent2.select_clear()submit_button=Button(root,activebackground="#F83030",command=submit_handler,fg="#000000",text="submit!").place(x=50,y=240,anchor='nw')again_button=Button(root,activebackground="red",command=again_handler,fg="black",text="try again!").place(x=50,y=270,anchor='nw')root.mainloop()

运行效果

本文件与上一篇中的核心函数文件需要放在同一路径下。

图片被审核了,算了

下一步规划

词库的自动更新

转载地址:http://tkwai.baihongyu.com/

你可能感兴趣的文章
python中如何区分常量和变量
查看>>
python导入模块的4种方法
查看>>
linux more命令简单使用
查看>>
vi 编辑器超级简单且实用的命令
查看>>
python多行注释和跨行字符串
查看>>
linux终端输出彩色字体
查看>>
一个程序学会python的流程控制
查看>>
python中sys.exit() os._exit() exit() quit()的简单使用
查看>>
linux rm -f rm -rf 命令:删除文件和文件夹
查看>>
ubuntu-14.04.3下安装VMware Tools(虚拟机与主机之间直接复制粘贴)
查看>>
python编写登录接口
查看>>
python字符串处理常用方法
查看>>
linux echo 向文件中追加信息
查看>>
python列表常见方法
查看>>
python打印列表中指定元素的所有下标(5种方法)
查看>>
python 字典常见方法
查看>>
python字典复制(浅拷贝and深拷贝)
查看>>
python set集合的特点,功能and常见方法
查看>>
python set集合运算(交集,并集,差集,对称差集)
查看>>
python字符串replace()方法
查看>>