debugsaurav
debugsaurav

Reputation: 19

Pass variable onto another variable in Python, tkinter

I want to pass the value of fruit so that I can get it printed as your fruit is apple if apple is selected and your fruit is apple orange if both selected. Maybe I should be using array?

from tkinter import *
from tkinter import ttk
import tkinter as tk

app = tk.Tk()

def show():
    fruit=""
    if apple.get()==1:
        fruit = "apple"
    if orange.get()==1:
        fruit = "orange"
    
    msg = "your fruit is %s"%fruit
    print(msg)

Label(app, text="fruit selected").grid(row=4,column=0, sticky=W)
apple = IntVar()
Checkbutton(app, text="apple", variable=apple,command = show).grid(row=4,column=1, sticky=W)
orange = IntVar()
Checkbutton(app, text="orange", variable=orange).grid(row=4,column=2, sticky=W)


#size
app.title('Basic message')
app.geometry("700x500")

app.mainloop()

Upvotes: 1

Views: 100

Answers (2)

Saad
Saad

Reputation: 3430

There can be many ways of doing this but lets first do it in your example. Just by adding one line and some improvements to your show function will give you the desired results.

def show():
    fruit=""
    if apple.get() and orange.get():
        fruit = 'apple orange' 
    elif apple.get():
        fruit = "apple"
    elif orange.get():
        fruit = "orange"

    msg = "your fruit is %s"%fruit
    print(msg)

But the whole code can be improved, using a for loop to create different fruit Checkbuttons and saving their variable IntVar as var in their instance which will make it easy to access later, finally saving those Checkbuttons in a list to access them in show function. The logic is simple I'm just checking which checkbutton is ticked and add their text in the fruit variable.

from tkinter import *
from tkinter import ttk
import tkinter as tk

app = tk.Tk()
ckb_list = []

def show():
    f = ''
    for ckb in ckb_list:
        if ckb.var.get():
            f += ckb['text'] + ' '
    msg = "your fruit is %s"%f
    print(msg)

Label(app, text="fruit selected").grid(row=4,column=0, sticky=W)

for col, fruit in enumerate(('apple', 'orange', 'banana', 'mango')):
    ckb = Checkbutton(app, text=fruit, command=show)
    ckb.var = ckb['variable'] = IntVar()
    ckb.grid(row=4,column=col+1, sticky=W)
    ckb_list.append(ckb)

#size
app.title('Basic message')
app.geometry("700x500")

app.mainloop()

Upvotes: 2

akBeater
akBeater

Reputation: 106

You forgot to add command=show in orange checkbox. Also it would be good to only print a message if one or more checkboxes are checked.

from tkinter import *
from tkinter import ttk
import tkinter as tk

app = tk.Tk()

def show():
    fruit=""
    if apple.get()==1:
        fruit = "apple"
    if orange.get()==1:
        fruit = "orange"
    
    if orange.get()==1 or apple.get()==1:
        msg = "your fruit is %s"%fruit
        print(msg)

Label(app, text="fruit selected").grid(row=4,column=0, sticky=W)
apple = IntVar()
Checkbutton(app, text="apple", variable=apple,command = show).grid(row=4,column=1, sticky=W)
orange = IntVar()
Checkbutton(app, text="orange", variable=orange, command = show).grid(row=4,column=2, sticky=W)


#size
app.title('Basic message')
app.geometry("700x500")

app.mainloop()

I would also recommend to use a array or list, if you want to add more boxes. You can also consider to use a class for better "variable management"

Upvotes: 0

Related Questions