0

I have seen posts about this but they all have something to do with while (x) == True:

my function has none of this. These are in 2 separate files. Please help! Screenshot of pycharm

I know that it is a lot of code, but I have spent a few hours on this, and I really can't figure out why this is happening. I am hoping that posting this project in entirety, I can have the function print in a window. At this point, it does not even need to be a tkinter GUI, if you have any other GUI that you think might work, please, let me know. Thank you in advance.

CODE

import random

moves = ["RU", "RD", "LD", "LU", "BL", "BR", "DR", "DL", "FL", "FR", "UL", "UR"]
dir = ["", "2"]
slen = random.randint(25, 30)

def scramble_gen():
    scramble = [0] * slen
    for x in range(len(scramble)):
        scramble[x] = [0] * 2
    return scramble

def scramble_replace(ar):
    for x in range(len(ar)):
        ar[x][0] = random.choice(moves)
        ar[x][1] = random.choice(dir)
    return ar

def valid(ar):
    for x in range(1, len(ar)):
        while ar[x][0] == ar[x-1][0]:
            ar[x][0] = random.choice(moves)
    for x in range(2, len(ar)):
        while ar[x][0] == ar[x-2][0] or ar[x][0] == ar[x-1][0]:
            ar[x][0] = random.choice(moves)
    return ar


def sprint(ar):
    for x in range(len(ar)):
        print(str(ar[x][0]) + str(ar[x][1]), end= " ")

s = scramble_replace(scramble_gen())'''

NEW FILE

from tkinter import *
import random
from scrambler import *
window = Tk()
window.title("Bryson's Scrambler")
lbl = Label(window, text = sprint(valid(s)))
lbl.grid(column=0, row=0)

txt = Entry(window,width=10)

txt.grid(column=1, row=0)

def clicked():

    lbl.configure(text="Button was clicked !!")

btn = Button(window, text="Click Me", command=clicked)

btn.grid(column=2, row=0)

window.mainloop()

window.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
Bryson_T
  • 5
  • 3
  • I don't understand what you think "print a function" means. – Karl Knechtel Jan 24 '21 at 06:09
  • _"I really can't figure out why this is happening."_ - why _what_ is happening? You haven't explained what it's doing, and how that is different than what you expect. If you are getting an error, please copy and paste the full error message in your question. – Bryan Oakley Jan 24 '21 at 06:10
  • I don't understand what you want to do - if you want to display some text in GUI then don't use `print()` but create string and use this string to put in GUI (and eventually display it with `print()`). – furas Jan 24 '21 at 06:11

2 Answers2

1

You're going to have to be a bit more specific than this,

I can have the function print in a window.

which function are you trying to print into a window? If you're trying to update an existing window with whatever object the function is supposed to "print", then don't exactly print it and return the object itself. i.e. if you mean the function sprint(

Then you should be able to call the function from Tkinter, take what it returns and add it to the window, then refresh it.

0

Instead of using print() create single string and use return

def sprint(ar):
    text = ""

    for x in range(len(ar)):
        #print(str(ar[x][0]) + str(ar[x][1]), end= " ")
        text += str(ar[x][0]) + str(ar[x][1]) + " "

    return text

And then it will work

lbl = Label(window, text=sprint(valid(s)))

EDIT:

More readable

def sprint(ar):
    text = ""

    for a, b in ar:
        text += f'{a}{b} '

    return text

or shorter

def sprint(ar):
    return ' '.join(f'{a}{b}' for a,b in ar)
furas
  • 134,197
  • 12
  • 106
  • 148