#Python lists of functions with tkinter

37 messages · Page 1 of 1 (latest)

wooden haven
#

https://pym.dev/p/323we/
This code iterates labels and buttons using a list.
Can I iterate functions in a similar way to make use of command=[i]

I have not created all the functions yet but is there anything wrong with doing that ?

runic rapids
#

I don't see anything wrong with it

minor hedge
#

Looks good

warped canyon
# wooden haven https://pym.dev/p/323we/ This code iterates labels and buttons using a list. Ca...

you can even name things in tkinter and iterate through all widgets in lets say a frame.
You can jump with widget.master to a higher area e.g. your button is on the frame

So i wrote that above before looking into your script.
First this i saw on your script:
com =[com1,com2,com3]
com1-3 are not defined anywhere as far as i saw

i recommend you use a lil more OOP style with tkinter.
Why should you use more OOP, because tkinter is not thread safe, based on what your application should do, like paralell stuff with I/O or such, it can happen that your mainloop is not reacting anymore. And with OOP that is easyer to fix/grab or change behaviour in these cases.

Just some things i would have wanted to know earlyer when i started woth tkinter/ttkbootstrap^^

wooden haven
#

output_label is giving me error for output_label not defined. Depending on which button I click.

If I remove all the functions and the coms list and only learn from one function and with labels and buttons then output label doesn't error but doesn't produce any output either.
I'm not sure I understand where stdout is going ?

wooden haven
warped canyon
#

Basically you dont have access to variables outside of a scope.
There are like "main scopes" functions, global, local, inside of classes, methods
and like "smaller scopes" like in a if,else, while, for loop.

You can say when it has identation, its in another scope.
You can give top down variables, but not allways.

that means, output_label not defined = the var does not exist where you want to use it.
Remember code runs from top to bottom and from left to right.
This is one simple thing, but one of the most cruical parts in understanding coding, but hardly underrated from new programmers 😉

wooden haven
warped canyon
#

have you followed your code from top to bottom once?

warped canyon
wooden haven
#

the initial question didn't have functions because I didn't want to work those until I knew if this was a thing to do.

#

thanks

warped canyon
#

yeah a mistake many do is to ask a question and share an old script xD

we cant tell you the future^^

wooden haven
#

I see, but generically was about coms[functions list] so that was the main question. Technically it's been answered but now opens new questions.

#

I'll use the multiline quote for future if it's small etc.

#

I'm trying to read enough to end up with a sort of generic button interface for certain tasks that may be intermittent use.
Like some installing these cryptocoin nodes or something where the terminal command is long and they either forgot where they kept it, or where they used it or what directory etc. At least with a programmable button they can perhaps use the user input to past, save the command to the button and re-use it again in the future without scouring the web for the command they once used or saved to a notepad or something.

warped canyon
wooden haven
#

like they say you don't use it you lose it lol

warped canyon
#

true

#

but if the basics sit tight, its easyer to get back into even after years (personal experience with multiple languages)

I mean with sit tight, at that time you use it, you can be woken up in the night and answer to most basic questions directly 😄

wooden haven
#

"""# Import the necessary module
import tkinter as tk
import subprocess

def run_com1():
command = "wine notepad" # Replace with your desired command
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output_label.config(text=result.stdout)

def run_com2():
command = "wine clock" # Replace with your desired command
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output_label.config(text=result.stdout)

def run_com3():
command = "wine cmd" # Replace with your desired command
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output_label.config(text=result.stdout)

Create a Tkinter window

window = tk.Tk()
window.geometry("720x250")
window.title("rFactor2 Server Helper")

Define labels and buttons

labels = ["Label 1", "Label 2", "Label 3"]
buttons = ["Notepad", "Clock", "Wine Command Prompt"]
coms=[run_com1,run_com2,run_com3]

Iterate over the labels and buttons

for i in range(len(labels)):

Create a Label widget for each label

label = tk.Label(window, text=labels[i])

Create a Button widget for each button

button = tk.Button(window, text=buttons[i], command=coms[i])

Use grid layout manager to align widgets in rows and columns

label.grid(row=i, column=0, padx=10, pady=10)
button.grid(row=i, column=1, padx=10, pady=10)

Start the Tkinter event loop

window.mainloop()
"""

#

Yes, it won't take long. Years ago I had to read and read before even unstanding what I was reading. No one to teach me, and like reading a dictionary to try to understand a language. So I have some understanding of OOP in general and can write some things.

#

I'm thinking output_label.config(text=result.stdout) with same name in other functions might be causing a problem. I don't think it should but could think I'm trying to call out of scope to another function with var of same name or something. ??

#

I see it now. I kept reading not defined and wasn't paying close enough attention. Sometimes the interpreter says things that really are not literal but caused by another problem. This time it was more literal. Thanks

wooden haven
#

I need to put it in the for loop and grid() I think I see it.

warped canyon
wooden haven
#

ahhh ok. because I'm on windows, but my app and .py is on vm, so I have to keep switching to windows and open .py in notepad. Proabably why.
I have it worked out atm. I didn't define the var in the loop or where it could be accessed. like you said scope issue. thanks. I'll post back and open .py with idle or something instead and select python in the pastbin or whatever.

#

working atm. so wondering if more is needed for seeing terminal messages or will this show it all.

warped canyon
#

if you work with gui you need to show stuff in gui not terminal 😄 otherwise no smart UX design

wooden haven
#

But how to interact with then terminal from then gui.
User needs to put system password or respond y/n sometimes.
I am working up to this part. But how to know if command completes when no output to label. Or if pending user input for terminal in the backround etc.

#

I definately prefer output to the label with user interaction to the terminal from within the gui but reading about it seems to be either some other python module lile pip install or something.

warped canyon
#

ok first of all, program in parts. like a modular system.

Then you can use a function qhere you give smth. to it, do stuff and return stuff.

Thats basically all a program does.

If you dont know, how to get smth. from program A to program B, then you have an understanding problem how A or B works to properly talk to each other.

Gui is basically only a fancy name for a lot of fancy things, that do nothing. All functionality is done in the background. so your gui is A, your backend B and your external program C.
You need to give your data from A to B and from B convert it as needed and give it to C