本文共 3389 字,大约阅读时间需要 11 分钟。
与其他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()//主事件循环
标签控件,即为一段文字,没有任何交互,最为简单,先上代码
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为定位锚点。具体定位方法可查阅相关资料。
用于向程序中输入字符串
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用于创建一个空白区域,以显示图形或文本,用法参见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/