0

I've tried working on this project for a while and am almost done but i cant figure out the control flow. when i call the function main_menu() it is supposed to give two buttons and if one is pressed it changes the choice to 1 or 2 and based off that it should display a new board. However it never moves to the if statement. Basically how do I execute the line of code (choice ==1) if i press the easy button. Any help would be appreciated.

import tkinter as tk
import random
import re

root = tk.Tk()

def scramble_board():
    new_board = ''.join(str(item) for i in board for item in i)
    shuffle_board = ''.join(random.sample(new_board, len(new_board)))
    new_shuffle = list(shuffle_board)
    return(new_shuffle)

def getWords(counter):
    first_letter = board[0][counter]
    last_letter = board[3][counter]
    new_words = [word for word in words.split() if(word.startswith(first_letter) and word.endswith(last_letter))]
    if(len(new_words) == 0):
        return('')
    else:
        return(random.choice(new_words))

def choice1():
    global choice
    choice = 1
    #clear_screen()
    return choice

def choice2():
    global choice
    choice = 2
    #clear_screen()

def clear_screen():
    for i in root.winfo_children():
        i.destroy()

choice = 3    
HEIGHT = 600
WIDTH = 600
root.geometry("600x600")
var = tk.IntVar()

canvas = tk.Canvas(root)
canvas.pack()

def main_menu():
    clear_screen()
    easy_button = tk.Button(root, text = "Easy", command = lambda: choice1())
    easy_button.place(relx =.43, rely =.3, width = 85, height = 35)
    med_button = tk.Button(root, text = "Medium", command = lambda: choice2())
    med_button.place(relx = .43, rely = .6, width = 85, height = 35)
    #easy_button.wait_variable(var)
    #med_button.wait_variable(var)
    root.wait_variable(var)


#while(choice ==3):
main_menu()
    #easy_button.wait_variable(var)
    #med_button.wait_variable(var)
    #root.wait_variable(var)

#while(choice != 3):
if(choice==1):

    print('hello')
    len_of_word = 3
    board = [["a", "a", "a"], ["a", "a", "a"], ["a", "a", "a"]]
    complete = False
    file_3 = "word_list_4.txt"
    file = open(file_3)
    words = file.read()
    pre_word = ["a", "a", "a"]

    while(complete == False):
        count = 0
        for i in range(len_of_word):
            word = (list(random.choice(open(file_3).read().split())))
            for j in range(len_of_word):
                board[i][j] = word[j]
        for i in range(len_of_word):
            for j in range(len_of_word):
                pre_word[j] = board[j][i]
                word_check = ''.join(pre_word)
            if word_check in words:
                count = count + 1
                if(count ==3):
                    print(board)
                    complete = True
        shuffled = scramble_board()            
        shuffled

    clear_screen()
    #frame is colored bit inside of canvas, .place is used to set the frame relative to the window
    #if the window is stretched by the user so will the frame.
    frame_game_screen = tk.Frame(root, bg = 'gray')
    frame_game_screen.place(relx = .2, rely = .1, relwidth = .6, relheight = .6)
benjawmin
  • 13
  • 5
  • 1
    Calling `.wait_variable(var)` is just going to hang forever if nothing ever calls `var.set()`. The ordinary Python variable `choice` that you actually are changing cannot end the wait. – jasonharper Feb 12 '20 at 21:20
  • where would i put var.set? within the choice1 and choice2 function. That makes the function call for main menu not run though – benjawmin Feb 12 '20 at 21:31
  • @benjawmin: First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) and [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – stovfl Feb 12 '20 at 22:37
  • @stovfl I did get the button to display then next window using .set() but now I'm running into problems looping the entire program. Is this where needing to understand mainloop helps or were you referring to something else – benjawmin Feb 12 '20 at 23:40
  • @benjawmin As it stands, you are using **top down** codeflow and a `while ...` loop which prevents the `tkinter.mainloop()` from running. Read also [While Loop Locks Application](https://stackoverflow.com/questions/28639228/python-while-loop-locks-application) – stovfl Feb 13 '20 at 00:09

1 Answers1

0

Here's a solution for you:

All you need to do is to take the following two steps:

  • Put the if statement inside a function(For Example: if_func) somewhere before choice1 and choice2, globalize boards and words. Like This:
def if_func():
    global board, words

    if choice == 1:

        print('hello')
        len_of_word = 3
        board = [["a", "a", "a"], ["a", "a", "a"], ["a", "a", "a"]]
        complete = False
        file_3 = "word_list_4.txt"
        file = open(file_3)
        words = file.read()
        pre_word = ["a", "a", "a"]

        while complete == False:
            count = 0
            for i in range(len_of_word):
                word = (list(random.choice(open(file_3).read().split())))
                for j in range(len_of_word):
                    board[i][j] = word[j]
            for i in range(len_of_word):
                for j in range(len_of_word):
                    pre_word[j] = board[j][i]
                    word_check = ''.join(pre_word)
                if word_check in words:
                    count = count + 1
                    if count == 3:
                        print(board)
                        complete = True
            shuffled = scramble_board()
            shuffled

        clear_screen()

        # frame is colored a bit inside of canvas, .place is used to set the frame relative to the window
        # if the window is stretched by the user so will the frame.

        frame_game_screen = tk.Frame(root, bg='gray')
        frame_game_screen.place(relx=.2, rely=.1, relwidth=.6, relheight=.6)
  • Call this function(if_func) inside your choice1 and choice2 functions. Like This:
def choice1():
    global choice
    choice = 1
    # clear_screen()
    if_func()


def choice2():
    global choice
    choice = 2
    if_func()

Note: Putting if inside a function will make you able to use it as many times as you want. Outside a function it would only be executed once when you run the program, hence it would make no difference.

DaniyalAhmadSE
  • 807
  • 11
  • 20