gui design example based on tkinter (beast speech generator v1.0.0)
Take the beast first speech generator as an example
Previous article address, beast speech generator v0.1.0
Main event cycle
Similar to other gui window implementations, it is based on a circular refresh of the main window, and then in the tkinter library, which inherits from the Tk() class. The code is as follows:
from tkinter import * from tkinter import messagebox import article_maker //Introduce core algorithm root=Tk()//Main window inheritance root.title('inm article creator')//title Property specification window title root.geometry('500x300')//geometry Property to set the window size root.mainloop()//Main event cycle
Label control
Label control, that is, a piece of text, without any interaction, the simplest, first code
l1=Label(text="theme:") l2=Label(text="Word number:") l1.place(x=0,y=20,anchor='nw') l2.place(x=0,y=40,anchor='nw')
As shown in the code, the Label control inherits from the Label class. The attribute text is the text displayed by the Label, place is the Label location, and anchor is the anchor. For specific positioning methods, please refer to relevant materials.
Entry control
Used to enter a string into a program
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')
Note that the construction of the entry class and the place method cannot be used at the same time, which has side effects. See: Article link
The entry class also has a get() method to get strings. See Button control for usage.
Canvas control
canvas is used to create a blank area to display graphics or text. See Button control for usage
Button control
The button control involves the position of the control, display text, display color, color after clicking, method call after clicking, etc., and the code of the button body first:
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')
In the button constructor, root indicates the main window it belongs to, activebackground indicates the color displayed when it is clicked, command is the method called by clicking the button, text is the displayed text, and place() method determines the location.
Here is the method called
def submit_handler(): title=ent1.get() num=ent2.get() //get()Method from entry Get string from if(num.isdigit()): article_maker.main(["lal",num,title]) messagebox.showinfo("Tips","Article generated successfully") //messagebox Show message box 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] //Read the first ten lines of the generated article and display them on the canvas curfile.close() cv=Canvas(root,bg='white') cv.place(x=120,y=20,anchor='nw') cv.create_text(200,100,text=tmp,font="Blackbody,16",fill="Black") else: ent2.select_clear() messagebox.showwarning("warning","The number of words needs to be a legal number!") def again_handler(): ent1.select_clear() ent2.select_clear()
There is a bug in the again handler. You can't clear the entry after clicking it. I wonder if you can give me some advice.
Holistic document
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="theme:") l2=Label(text="Word number:") 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("Tips","Article generated successfully") 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="Blackbody,16",fill="Black") else: ent2.select_clear() messagebox.showwarning("warning","The number of words needs to be a legal number!") 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()
Operation effect
This file and the core function file in the previous article need to be placed in the same path.
Next step planning
Automatic update of Thesaurus