is a simple illustration of the Hanoi game, we want to move the top of the x column above the z axis (Hanoi game rules can be self-search, here do not introduce)
The Tkinter and Scrolledtext modules need to be introduced, and the code below is directly labeled (I use python3.6).
1 ImportTkinter2 fromTkinter.scrolledtextImportScrolledtext3Root =Tkinter. Tk ()4Root.title ('Hanoi problem Solver')5Root.geometry ('300x200')6Root.resizable (Width=false, height=True)7Tkinter. Label (Root, text='Please enter the number of Hanoi layers', Font= ('Microsoft Ya-Black', 12) . Pack ()8var =Tkinter. Stringvar ()9E = Tkinter. Entry (root, textvariable =var)Ten E.pack () Onet =Tkinter.scrolledtext.ScrolledText (Root) AAns = [] - defHanoi (n, x, Y, z): - ifn = = 1 : theAns.append (x +' -'+z) - Else : -Hanoi (n-1, X, Z, y)#move the first n-1 plate from X to y -Ans.append (x +' -'+ z)#move the bottom of the last plate from X to Z +Hanoi (n-1, y, X, z)#Move the n-1 plate on y to Z . - returnans + defCalc (): A delans[:] atn =Int (var.get ()) -str2 = Hanoi (n,'x','y','Z') -T.delete (0.0, Tkinter. END) -Count =0 - forStreinchstr2: -Count = Count + 1 inT.insert (Tkinter. END,'Section'+ STR (count) +'Step:'+ Stre +'\ n') -T.insert (1.0,'[Altogether'+ STR (count) +'Step]\n') to T.pack () +Tkinter. Button (Root, text="Determine", command=calc). Pack () -Root.mainloop ()
Then after running the results such as the number of layers of input Hanoi is 3, and then click the OK button below will show the detailed
Steps.
(PS: Write this is to make notes to prevent later forget, problem-solving ideas code part of the results of the online search, such as small turtle's Python teaching, if there is infringement contact I immediately delete, thank you!! )
Python writes a simple graphical interface Hanoi Solver