user6665922
user6665922

Reputation:

Python tkinter classes changing icon

I'm not that familar with classes or tkinter, but what i'm trying to do is, if test1 is selected, than change the icon too favicon, i can't really figure it out, if you could help me that would be great, and if i'm doing something wrong

from tkinter import *


class GUI:
    def __init__(self, master):

        self.iconnum = IntVar()
        master.title('Testing')
        master.resizable(width=False, height=False)
        master.maxsize(500, 250)
        master.minsize(500, 250)

        self.test1= Radiobutton(master, text="test1", variable=0, value=1, )
        self.test2= Radiobutton(master, text="test2", variable=0, value=2, )
        self.test3= Radiobutton(master, text="test3", variable=0, value=3, )
        self.test4= Radiobutton(master, text="test4", variable=0, value=4,  )

        self.test1.grid(row=2, columnspan=1)
        self.test2.grid(row=2, columnspan=2)
        self.test3.grid(row=2, column=1)
        self.test4.grid(row=3, columnspan=1,)

        self.Test5= Radiobutton(master, text="Test5", indicatoron=0, height=1, width=35,  value=0, command=self.icon_switcher)
        self.Test6= Radiobutton(master, text="Test6", indicatoron=0, height=1, width=35, value=1, command=self.icon_switcher)

        self.Test5.grid(row=1)
        self.Test6.grid(row=1, column=1,)

    def icon_switcher(self):
        if self.iconnum == 1:
            self.master.iconbitmap('favicon.ico')




root = Tk()
gui = GUI(root)

root.mainloop()

enter image description here

Upvotes: 0

Views: 408

Answers (1)

Dashadower
Dashadower

Reputation: 662

You need to give a tkinter IntVarfor RadioButton's variable keyword:

Change

self.test1= Radiobutton(master, text="test1", variable=0, value=1, )

to

self.test1= Radiobutton(master, text="test1", variable=self.iconnum, value=1, )

This will record the selected value in the IntVar. Then in the icon_switcher function you can call self.iconnum.get() to get the selected value.

Upvotes: 2

Related Questions