#๐Ÿ”’ how do i accept input while showing img?

104 messages ยท Page 1 of 1 (latest)

covert granite
#

the relevant code part:

def display_img(root:tk.Tk,img:str) -> None:
    img=tk.PhotoImage(file=img)
    panel = tk.Label(root, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    root.mainloop()

def run_automat(automat: Automat):
    automat.end()
    root=tk.Tk()
    root.configure(background="black")

    display_img(root,automat.draw())
    i=input("next:")
    while i!="":
        automat.step(i)
        automat.draw()

        i=input("next:")
    print(automat.end())

def execute_str(s: str, automat: Automat) -> bool:
    automat.end()
    for i in s:
        automat.step(i)
    return automat.end()

while True:
    run_automat(automat)

when i show the state it freezez and i cant input the next letter

oblique roostBOT
#

@covert granite

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

steady thorn
#

use a gui widget for getting user's input like tk.Entry

#

you also shouldn't be calling your mainloop from a function

#

make it globally

covert granite
#

i dont want to make my code around the UI but fine

steady thorn
#

if you wrote functions with clear inputs and outputs, it wouldn't be too difficult

#

if you wrote messy code to begin with, then unfortunately you're going to have to make adjustments

covert granite
#

i dont think its that messy

steady thorn
#

typically you would keep your ui functions and your other "controller" functions separate rather than try and smush them together

#

the UI is just a view for the data that you're creating

#

so instead of data coming from input(), it would come from tk.Entry

#

instead of data being printed, it gets shown as a label

#

etc

covert granite
steady thorn
#

UI is basically a big fancy while loop

#

that's what mainloop() is

#

so any runtime functionality must happen from a function call

#

if you have a while loop somewhere else while the UI is already running, it will block the UI event loop and freeze it up

covert granite
#

is there a way to read an input without waiting for an enter? like letter by letter?

steady thorn
#

you could, but can I ask why?

covert granite
#

bc automats have an abc and act for each letter

steady thorn
#

I'm not really sure what you mean by that and I'm not sure what an automat is

covert granite
#

so it be simple if enter was the end of word command and anything else would be instantly used

#

have u ever seen something like this before?

steady thorn
#

nope

covert granite
#

its a cellular automat

#

or state machine

#

it has states and based on those states + a letter they decide whitch node to go to

#

some regex interpreters do this too

#

what u see is an automat made to accept numbers devisible by 3 in base 2

steady thorn
#

ok so what inputs do you need from the user?

covert granite
#

letters from the automats abc

#

in this case 0 or 1

steady thorn
#

so the user only has to type 0 or 1?

covert granite
#

in this case yes but its based on the automats abc

steady thorn
#

ok so why do you need to update it as it is typed without needing to press enter?

covert granite
steady thorn
#

you could create a button instead

#

then they would click 0 or 1

covert granite
#

too

steady thorn
#

you could make it dynamic so a button is created per valid input

covert granite
steady thorn
#

I'm not sure your exactly attribute setup to access that data but you can create buttons from a loop

covert granite
#

and how do i make sure no buttons or the picture owerlap?

steady thorn
#
for valid in automat.abc_values:
    button = tk.Button(values_frame, text=valid)
    button.pack()
steady thorn
covert granite
steady thorn
#

you would have to create them

#

frames are organizational widgets

#

they're like containers that can hold other widgets

#

so you could put all the valud buttons into 1 frame

#

your image viewer into another frame

#

any other entries/buttons into another

#

then you pack or grid all the frames into your window

covert granite
#

so like
sidebysideframe(pillarframe(buttons),picture) something like this if it exists?

#

also why doesnt this make the window backround black?
root.configure(background="black")

steady thorn
#

when you make any widget, the first argument is the "master" which is what other widget it should be a part of

#

so you make your frames with the root as the master

#

and you can add your other widgets to the frames

steady thorn
#

you can nest them too

#

frames can hold other frames

#

if you had a drawing mockup of what you want your UI to look like, it would be really helpful

covert granite
#

yeah kinda like frames or what in MIT appinventor?

steady thorn
#

not sure what that is

steady thorn
covert granite
steady thorn
#

so the buttons on the left are the value buttons? Any other buttons needed?

steady thorn
#

ok so you can think of this window as 2 frames

#

1 frame for the buttons, another frame for the image viewer

covert granite
#

why an extra frame fro the image? why not put it on root?

steady thorn
#

just keeps things cleaner if you change your mind about things

#

I usually throw extra frames at things to futureproof it

covert granite
#

makes some sense

steady thorn
#

also tkinter uses pack or grid to add its elements to the UI

#

you cannot mix and match WITHIN the same "geometry manager"

#

so you can pack a frame into your root, and then the root can have gridded widgets

#

using frames gives you way more control of how widgets get laid out

#
import tkinter as tk 

root = tk.Tk()
root.geometry('500x500')

button_frame = tk.Frame(root, bg='red')
image_frame = tk.Frame(root, bg='blue')

button_frame.grid(column=0, row=0, sticky='news')
image_frame.grid(column=1, row=0, sticky='news')

root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=3)
root.grid_rowconfigure(0, weight=1)


root.mainloop()
#

try running this example

#

There we go, a nice beautiful image viewer

covert granite
#

im back just my parents came home and forced me to eat

covert granite
#

what does pack do what do i have to pack and when?

steady thorn
#

we use pack or grid to add it

#

.rp tkinter

slender heraldBOT
#

Here are the top 5 results:

Python GUI Programming With Tkinter
Build a Tic-Tac-Toe Game With Python and Tkinter
Bypassing the GIL for Parallel Processing in Python
Python sleep(): How to Add Time Delays to Your Code
Arduino With Python: How to Get Started
steady thorn
#

I'd really recommend this first link here

#

it covers a lot of the tkinter basics and it will make all of this a lot less painful

covert granite
#

oh so i pack the buttons one by one?

steady thorn
#

I need to get going, but I'll leave you with the code I have

#
import tkinter as tk 

root = tk.Tk()
root.geometry('500x500')

button_frame = tk.Frame(root, bg='#442222')
image_frame = tk.Frame(root, bg='blue')

button_frame.grid(column=0, row=0, sticky='news')
image_frame.grid(column=1, row=0, sticky='news')

root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=3)
root.grid_rowconfigure(0, weight=1)

for i in range(8):
    button = tk.Button(button_frame, text=f'Button {i+1}', padx=5, pady=5, bg='#222222', fg='#eeeeee', font=('segoe ui', 12))
    button.pack(pady=5)

img = tk.PhotoImage(file='mountains.png').subsample(2)
img_viewer = tk.Label(image_frame, image=img, bg='#222222')
img_viewer.pack(expand=True, fill='both')


root.mainloop()
#

you'll have to replace the image file with something local to you

oblique roostBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.