Python Forum
how I can use the widgets as an argument of a function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how I can use the widgets as an argument of a function?
#1
In this example, I have an entry and two LabelFrames. I am looking to find out how I can call my LabelFrame through only one function. I have written this code as an example.

import tkinter as tk


class Data:
    def __init__(self):
        self.number = tk.StringVar()


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.minsize(700, 700)
        container = tk.Frame(self)
        container.pack()

        self.data = Data()

        self.frames = {}
        for F in (PageOne, ):
            frame = F(container, self.data)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

    def show_frame(self, c):
        frame = self.frames[c]
        frame.tkraise()


class PageOne(tk.Frame):
    def __init__(self, parent, data):
        super().__init__(parent)
        self.data = data

        entry1 = tk.Entry(self, textvariable=self.data.number)
        entry1.pack()

        self.button1 = tk.Button(self, text="click", command=self.add)
        self.button1.pack()

        self.frame1 = tk.LabelFrame(self, height=200, width=200, borderwidth=2)
        self.frame1.pack()

        self.button2 = tk.Button(self, text="click", command=self.add)
        self.button2.pack()

        self.frame2 = tk.LabelFrame(self, height=200, width=200, borderwidth=2)
        self.frame2.pack()

    def add(self):
        self.frame1.config(text=str(self.data.number.get()))


app = SampleApp()
app.mainloop()
Currently, I have used add() function to add a title for frame1 through button1. But how I can add a title for the other frame using the add() function again through button2. How I can get any frame as an argument through the function?
Reply
#2
How will the program know that you want to give a name to frame1 instead of frame2?
Reply
#3
(Apr-21-2022, 09:03 PM)Gribouillis Wrote: How will the program know that you want to give a name to frame1 instead of frame2?

That is also my question. currently it is working for the one I have defined in the add() function. or I have to define the other one in another function and another button. But, I am looking for a way to give the frame name in only one function through a new argument.
Reply
#4
No I don't mean from the code's perspective, but from the user perspective. How will the user let the program know that they want one frame instead of another?

For example, the user could type 1 spam in the entry and the program would know that 'spam' is the new title of frame 1 an 2 ham to tell the program that the target is frame 2. The way you envisage the interaction has an impact on the code.
Reply
#5
(Apr-21-2022, 09:24 PM)Gribouillis Wrote: No I don't mean from the code's perspective, but from the user perspective. How will the user let the program know that they want one frame instead of another?

For example, the user could type 1 spam in the entry and the program would know that 'spam' is the new title of frame 1 an 2 ham to tell the program that the target is frame 2. The way you envisage the interaction has an impact on the code.

this is an example I raised for my bigger problem. I have a function that is around 60 lines. I don't want to rewrite that again. I want to call that function, but inside that, I want to get a new Frame to do some stuff in that frame. for example, I have introduced multiple frames. I want to be able to use this function and show my result in a specific frame again. This is not something a new user decides on that. I will do that inside the code.
Reply
#6
60 lines isn't much. Let's see the function and you can add some comments to highlight the important parts.
Reply
#7
(Apr-22-2022, 12:25 AM)deanhystad Wrote: 60 lines isn't much. Let's see the function and you can add some comments to highlight the important parts.
I tried to bring the part of my code with this problem, but everything is linked and It is not possible to bring all. I tried to update my question and my post to make it easier to understand what I am looking for. Thanks
Reply
#8
Are you trying to make LabelFrame behave like it has a "textvariable" argument similar to Label? You can do that by creating the label yourself.
import tkinter as tk

def increment(var):
    var.set(var.get() + 1)

root = tk.Tk()
frame_var = tk.IntVar(value=0)
label = tk.Label(textvariable=frame_var)
frame = tk.LabelFrame(root, labelwidget=label)
frame.pack(fill="both", expand="yes", padx=5, pady=5)
 
button = tk.Button(frame, text="Press to change frame label", command=lambda: increment(frame_var))
button.pack(padx=5, pady=5)
 
root.mainloop()
Reply
#9
(Apr-23-2022, 05:02 PM)deanhystad Wrote: Are you trying to make LabelFrame behave like it has a "textvariable" argument similar to Label? You can do that by creating the label yourself.
import tkinter as tk

def increment(var):
    var.set(var.get() + 1)

root = tk.Tk()
frame_var = tk.IntVar(value=0)
label = tk.Label(textvariable=frame_var)
frame = tk.LabelFrame(root, labelwidget=label)
frame.pack(fill="both", expand="yes", padx=5, pady=5)
 
button = tk.Button(frame, text="Press to change frame label", command=lambda: increment(frame_var))
button.pack(padx=5, pady=5)
 
root.mainloop()

Thanks for your reply. No, I am trying to use the same function and show the results in another Frame. So, I think I need to add an argument to my function to get my frame.
Reply
#10
Using only this as a guide:
Quote:Currently, I have used add() function to add a title for frame1 through button1. But how I can add a title for the other frame using the add() function again through button2. How I can get any frame as an argument through the function?
Does this do what you want?
def add(self, frame, data):
     frame.config(text=str(data.get()))
...
        self.button1 = tk.Button(self, text="click", command=lambda : self.add(self.frame1, data.number)
        self.button1.pack()
 
        self.button2 = tk.Button(self, text="click", command=lambda : self.add(self.frame2, data.number)
        self.button2.pack()
add() doesn't even have to be a method, it could be a function because it is not using "self".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax Error: Positional argument follows keyword argument Rama02 3 4,133 Feb-09-2021, 06:10 PM
Last Post: deanhystad
  [PyQt] how to pass tablewidget object like argument for function atlass218 2 2,280 Mar-02-2020, 06:53 AM
Last Post: atlass218

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020