Home > other >  Tkinter - How to pass a variable using modules?
Tkinter - How to pass a variable using modules?

Time:09-17

I need to take a value from user and then pass this value into function to get a string with this values. My code in first file looks like below (of course it is simplified version):

import tkinter as tk
from tkinter import ttk
import myfunction  # this is my module that has another function

class Interface(ttk.Frame):

    def __init__(self, container):
        super().__init__(container)

        self.user_price_minimum = tk.StringVar()

        minimum_price = ttk.Label(self, text="Minimum price is: ")
        minimum_price.grid(row=0, column=0, padx=5, pady=5)
        minimum_price_entry = ttk.Entry(self, width=15, textvariable = self.user_price_minimum)
        minimum_price_entry.grid(row=0, column=1)
        minimum_price_entry.focus()

        #############
        button = ttk.Button(self, text="Use price")

        button.grid(column=0, rows=2, columnspan=2, padx=5, pady=5)

root = tk.Tk()
root.geometry("450x250")
root.title("Looking for a flat")
root.columnconfigure(0, weight=1)

frame = Interface(root)
frame.pack()

root.mainloop()

My another python file that calls myfunction.py should be able to take this minimum_price from user and add this into string. Code looks like below:

def minimum_price(self):
    price_min = self.user_price_minimum.get()
    price_min = int(price_min)

    print(f'Minimum price is: {price_min}')

So I am not sure how I could use minimum_price values from user into this function.

CodePudding user response:

The simplest is to do this:

button = ttk.Button(self, text="Use price", command=lambda: myfunction.minimum_price(self))

However you could also define it as a method in the class itself:

class Interface(ttk.Frame):
    
    def __init__(self, container):
        super().__init__(container)
    
        self.user_price_minimum = tk.StringVar()
            
        minimum_price = ttk.Label(self, text="Minimum price is: ")
        minimum_price.grid(row=0, column=0, padx=5, pady=5)
        minimum_price_entry = ttk.Entry(self, width=15, textvariable = self.user_price_minimum)
        minimum_price_entry.grid(row=0, column=1)
        minimum_price_entry.focus()
        
        #############
        button = ttk.Button(self, text="Use price", command=print_price)
        button.grid(column=0, rows=2, columnspan=2, padx=5, pady=5)  
        
    def print_price(self):
        price = self.user_price_minimum.get()
        print(f'Minimum price: {price}')

My personal preference (when using another file in such a case like this one (tho I would probably prefer to define this as above)) would be if:

# in the other file
def minimum_price(value):
    print(f'Minimum price: {value}')

# inside the class
button = ttk.Button(self, text="Use price", command=lambda: myfunction.minimum_price(self.user_price_minimum.get()))

Also in this case you don't necessarily need the StringVar, you could also simply get the value by using:

# assignment
self.minimum_price_entry = ttk.Entry(self, width=15)

# get value (probably in some function call, basically the same way as with the `StringVar` except less code)
self.minimum_price_entry.get()
  • Related