简体   繁体   中英

python control flow of tkinter widgets

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM