#Python lists of functions with tkinter
37 messages · Page 1 of 1 (latest)
I don't see anything wrong with it
Looks good
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^^
What about this ? I get error for output but I think something to do with the same named output or something.
Only erros with multple functions not with a single one.
Here is the update attempt.
https://pym.dev/p/2gcbj/
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 ?
So you think ditching the for loop and just go straight at it would be best.
I was doing that for alignment mostly, but figured why not throw the commands in there with it.
Hard to focus on best method actually.
well you heared about scopes/scoping?
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 😉
I see, so is calling the function with that var outside of scope ?
I understand your reply but lack understanding of why this is outside of scope. Perhaps i should make the global var ?
have you followed your code from top to bottom once?
for this it looks good tho. your functions are existing which you link in the list.
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
btw maybe share your script (cuz its short) in here with codeblocks:
https://www.geeksforgeeks.org/how-to-format-text-into-code-block-on-discord/
Here are the following steps to format text into code block on discord : Step 1: Open Discord ;Step 2: Navigate to the server;
yeah a mistake many do is to ask a question and share an old script xD
we cant tell you the future^^
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.
you can even make click events with tkinter 😉
Like if you click & shift it will execute directly 😄
There is so much you can do, but i think in your case, its best to look up the basics a little more.
You will learn that you need to refactor so or so after periods of time, because your skills increase daily when working from the base up 🙂
I once had much more knowledge about tkinter and coding in general but it's been so long I forgot what little I knew.
I created gui apps on tkinter in the past but now it's like starting all over again.
like they say you don't use it you lose it lol
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 😄
"""# 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
I need to put it in the for loop and grid() I think I see it.
look at this please and reformat your message 😄
Hard to read like that or your paste site, which is not formatting it properly
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.
should work here at the past morsels etc.
https://pym.dev/p/2ehpn/
working atm. so wondering if more is needed for seeing terminal messages or will this show it all.
if you work with gui you need to show stuff in gui not terminal 😄 otherwise no smart UX design
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.
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