Python writes a simple graphical interface Hanoi Solver

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.