Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Simple registration form using Python Tkinter
Next article icon

Prerequisites: 

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinteris the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Now, it’s up to the imagination or necessity of the developer, what he/she wants to develop using this toolkit. 
To create a Tkinter : 

  • Importing the module – Tkinter
  • Create the main window (container)
  • Add any number of widgets to the main window.
  • Apply the event Trigger on the widgets

The GUI would look like below:


Let’s create a GUI version of a simple FLAMES game.FLAMES is a popular game named after the acronym: Friends, Lovers, Affectionate, Marriage, Enemies, Sibling. This game does not accurately predict whether an individual is right for you, but it can be fun to play this with your friends.
 

Below is the implementation :
 

Python3
# import all functions from the tkinterfromtkinterimport*# function for removing common characters# with their respective occurrencesdefremove_match_char(list1,list2):foriinrange(len(list1)):forjinrange(len(list2)):# if common character is found# then remove that character# and return list of concatenated# list with True Flagiflist1[i]==list2[j]:c=list1[i]# remove character from the listlist1.remove(c)list2.remove(c)# concatenation of two list elements with *# * is act as border mark herelist3=list1+["*"]+list2# return the concatenated list with True flagreturn[list3,True]# no common characters is found# return the concatenated list with False flaglist3=list1+["*"]+list2return[list3,False]# function for telling the relationship statusdeftell_status():# take a 1st player name from Player1_field entry boxp1=Player1_field.get()# converted all letters into lower casep1=p1.lower()# replace any space with empty stringp1.replace(" ","")# make a list of letters or charactersp1_list=list(p1)# take a 2nd player name from Player2_field entry boxp2=Player2_field.get()p2=p2.lower()p2.replace(" ","")p2_list=list(p2)# taking a flag as True initiallyproceed=True# keep calling remove_match_char function# until common characters is found or# keep looping until proceed flag is Truewhileproceed:# function calling and store return valueret_list=remove_match_char(p1_list,p2_list)# take out concatenated list from return listcon_list=ret_list[0]# take out flag value from return listproceed=ret_list[1]# find the index of "*" / border markstar_index=con_list.index("*")# list slicing perform# all characters before * store in p1_listp1_list=con_list[:star_index]# all characters after * store in p2_listp2_list=con_list[star_index+1:]# count total remaining characterscount=len(p1_list)+len(p2_list)# list of FLAMES acronymresult=["Friends","Love","Affection","Marriage","Enemy","Siblings"]# keep looping until only one item# is not remaining in the result listwhilelen(result)>1:# store that index value from# where we have to perform slicing.split_index=(count%len(result)-1)# this steps is done for performing# anticlock-wise circular fashion counting.ifsplit_index>=0:# list slicingright=result[split_index+1:]left=result[:split_index]# list concatenationresult=right+leftelse:result=result[:len(result)-1]# insert method inserting the# value in the text entry box.Status_field.insert(10,result[0])# Function for clearing the# contents of all text entry boxesdefclear_all():Player1_field.delete(0,END)Player2_field.delete(0,END)Status_field.delete(0,END)# set focus on the Player1_field entry boxPlayer1_field.focus_set()# Driver codeif__name__=="__main__":# Create a GUI windowroot=Tk()# Set the background colour of GUI windowroot.configure(background='light green')# Set the configuration of GUI windowroot.geometry("350x125")# set the name of tkinter GUI windowroot.title("Flames Game")# Create a Player 1 Name: labellabel1=Label(root,text="Player 1 Name: ",fg='black',bg='dark green')# Create a Player 2 Name: labellabel2=Label(root,text="Player 2 Name: ",fg='black',bg='dark green')# Create a Relation Status: labellabel3=Label(root,text="Relationship Status: ",fg='black',bg='red')# grid method is used for placing# the widgets at respective positions# in table like structure .label1.grid(row=1,column=0,sticky="E")label2.grid(row=2,column=0,sticky="E")label3.grid(row=4,column=0,sticky="E")# Create a text entry box# for filling or typing the information.Player1_field=Entry(root)Player2_field=Entry(root)Status_field=Entry(root)# grid method is used for placing# the widgets at respective positions# in table like structure .# ipadx keyword argument set width of entry space .Player1_field.grid(row=1,column=1,ipadx="50")Player2_field.grid(row=2,column=1,ipadx="50")Status_field.grid(row=4,column=1,ipadx="50")# Create a Submit Button and attached# to tell_status functionbutton1=Button(root,text="Submit",bg="red",fg="black",command=tell_status)# Create a Clear Button and attached# to clear_all functionbutton2=Button(root,text="Clear",bg="red",fg="black",command=clear_all)# grid method is used for placing# the widgets at respective positions# in table like structure .button1.grid(row=3,column=1)button2.grid(row=5,column=1)# Start the GUIroot.mainloop()

Output : 
 


Simple FLAMES Game Using Tkinter
Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp