+

Python | A simple FLAMES game using Tkinter

Python offers several options for developing a graphical user interface. Of all the GUI methods, tkinter is the most commonly used method. It is the standard Python interface for the Tk GUI toolkit that ships with Python. Python with tkinter outputs the fastest and easiest way to build GUI applications. Now it`s up to the developer`s imagination or need what he / she wants to develop with this toolkit.

To create a tkinter:

  • Module import — tkinter
  • Create main window (container)
  • Add any number of widgets to the main window.
  • Apply Trigger event to widgets

Let`s create a Gui version of a simple FLAMES game. FLAMES — popular game named after the acronym: Friends, Lovers, Affectionate, Marriage, Enemies, Brother and Sister. This game cannot accurately predict if a person is right for you or not, but it can be fun to play this game with friends.

Below is the implementation:

# import all functions from tkinter

from tkinter import *

 
# function to remove common characters
# with their respective cases

def remove_match_char (list1, list2): 

  

for i in range ( len (list1)): 

for j in range ( len (list2) ): 

 

# if a common symbol is found

# then remove that symbol

# and return a chained list

# list with Type flag

  if list1 [i] = = list2 [j]: 

c = list1 [i] 

 

# remove character from list

  list1.remove (c) 

list2.remove (c) 

 

  # concatenate two list items with *

# * acts as a border mark here

list3 = list1 + [ "*" ] + list2 

 

# return merged list with True flag

return [list3, True

 

# no common characters found

# return the concatenated list with False flag

list3 = list1 + [ "*" ] + list2 

  return [list3, False

 

 
# function for determining relationship status

def tell_status (): 

 

# take the first player`s name from the Player1_field input field

p1 = Player1_field.get ()

 

# converted all letters to lowercase

p1 = p1.lower () 

 

# replace any space with an empty string

p1.replace ( "" , "") 

 

# make a list of letters or symbols

  p1_list = list (p1) 

  

  # take the name of the second player from the Player2_field input field

p2 = Player2_field.get () 

p2 = p2.lower ( ) 

p2.replace ( " " ," ") 

  p2_list = list (p2) 

 

# initially accepting a flag as True

proceed = True

 

# keep calling remove_match_char

# until common characters or

# keep looping until the continue flag is True

while proceed: 

 

# call the function and save the return value

ret_list = remove_match_char (p1_list, p2_list) 

  

  # remove the compound list from the return list

con_list = ret_list [ 0

 

# extract the flag value from the return list

proceed = ret_list [ 1

  

  # find index & quot; * & quot; / border mark

star_index = con_list.index ( "*"

 

# slice the list

 

# all characters before * store in p1_list

p1_list = con_list [: star_index] 

 

# all characters after * save to p2_list

p2_list = con_list [star_index + 1 :] 

 

 

# total remaining characters

count = len (p1_list) + len (p2_list) 

 

# list of abbreviations FLAMES

result = [ "Friends" , "L ove " , " Affection " , "Marriage" , "Enemy" , "Siblings"

  

# keep looping until just one element

# not left in the list of results

while len (result) & gt; 1

 

# save this index value from

  # where we should do the slicing.

  split_index = (count % len (result) - 1

 

# this step is done to complete

  # counterclockwise circular fashion count.

  if split_index & gt; = 0

 

# slicing list

  right = result [split_index + 1 :] 

  left = result [: split_index] 

  

# list of lists

result = right + left 

 

else

  result = result [: len (result) - 1

 

# insert insert method

  # value in the text input field.

Status_field.insert ( 10 , result [ 0 ])

  

 
# Function to clean up
# content of all text input fields

def clear_all (): 

Player1_field.delete ( 0 , END) 

  Player2_field.delete ( 0 , END) 

Status_field.delete ( 0 , END) 

 

# set focus to the Player1_field input field

Player1_f ield.focus_set () 

 

 
# Driver code

if __ name__ = = "__ main__"

 

# Create a GUI window

  root = Tk () 

 

# Set the background color of the GUI window

root.configure (background = `light green`

 

  # Set GUI window preferences

root.geometry ( "350x125"

  

# set the tkinter GUI window name

root.title ( "Flames Game"

 

# Create player 1 name: label

  label1 = Label (root, text = "Player 1 Name: "

  fg = `black` , bg = `dark green`

  

  # Create player name 2: label

label2 = Label (root, text = "Player 2 Name:"

fg = `black` , bg = `dark green`

  

# Create relationship status: label

labe l3 = Label (root, text = "Relationship Status:"

fg = `black` , bg = `red`

 

# the grid method is used to place

# widgets in corresponding positions

# in the table as 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 text input field

# to fill in or enter information.

Player1_field = Entry (root) 

Player2_field = Entry (root) 

Status_field = Entry (root)

 

# the grid method is used to place

# widgets in corresponding positions

  # in the table as a structure.

# ipadx The keyword argument sets the width of the input 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 attach

# tell_status function

  button1 = Button (root, text = "Submit" , bg = "red"

fg = "black" , command = tell_status) 

 

# Create a Clear button and attach

  # into the clear_all function

  button2 = Button (root, text = "Clear" , bg = "red"

fg = "black" , command = clear_all)

  

# grid method used for placement

# widgets in corresponding positions

# in the table as a structure .

button1.grid (row = 3 , column = 1

  button2 .grid (row = 5 , column = 1 )

  

  # Launch GUI

root.mainloop () 

Output:
/ p>

 

# grid method used to place

# widgets in corresponding positions

# in the table as a structure.

button1.grid (row = 3 , column = 1

button2.grid (row = 5 , column = 1 )

 

# Launch GUI

  root.mainloop () 

Exit:

/ p>

 

# grid method used to place

# widgets in corresponding positions

# in the table as a structure.

button1.grid (row = 3 , column = 1

button2.grid (row = 5 , column = 1 )

 

# Launch GUI

  root.mainloop () 

Exit:

Get Solution for free from DataCamp guru