基于tkinter的gui设计实例

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

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

上一篇文章地址,野獸发言生成器v0.1.0

主事件循环

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

from tkinter import *
from tkinter import messagebox

import 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 messagebox

import article_maker

root=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()

运行效果

本文件与上一篇中的核心函数文件需要放在同一路径下。
在这里插入图片描述

下一步规划

词库的自动更新

发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/103967276
今日推荐