Python Forum
Found buttonstate in tabs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Found buttonstate in tabs
#1
Is a total beginner when it comes to Python and Tkinter. I have programmed a lot of C # and VB over the years and thought of learning a litle Python for a project. It's progressing slowly and the code can be optimized, I know, but one thing at a time. :)

I want to make an app with 4 tabs. On each side I will have a bunch of buttons, a total of 50 on different tabs. I want the buttons as arrays when they send a UDP command where a group value is changed. So I do not want to build 50 functions that take care of each button, but have a general function. I'am also building a function that collect my buttons information from a Helvar router so they update when we turn on lights from a button outside this "app".

When I use without tabs, it works the way I want with:
     button = buttons [int (i)]
     if button ['relief'] == 'raised':
When I use tabs, I can't find my button. "TypeError: 'NoneType' object is not subscriptable"

I don't need someone that rewrite my code, I only need to knew if it's possible and some hint what to search on net to found a solution. If you get everything served, you do not learn, but it can help to be pushed in any direction. :)

My test code scaled down:
from tkinter import *
from tkinter import ttk
import tkinter as tk

def on_tab_selected(event):
    global selected_tab 
    selected_tab = event.widget.select()
    print(selected_tab)

root = tk.Tk()
root.title("MainWindow")
root.geometry("800x480")
root.configure(bg='black')

tabControl = ttk.Notebook(root)
tabControl.grid(row=1, column=0, columnspan=50, rowspan=49, sticky='NESW')

s = ttk.Style()
s.configure('TNotebook.Tab', font=('Helvetica','15','bold') )

tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab4 = ttk.Frame(tabControl)

tabControl.bind("<<NotebookTabChanged>>", on_tab_selected)

tabControl.add(tab1, text =' Start ')
tabControl.add(tab2, text =' Arbetsljus ')
tabControl.add(tab3, text =' Blåljus ')
tabControl.add(tab4, text =' Övrigt ')

tabControl.pack(expand = 1, fill ="both")

class buttonFunction():
    def __init__(self,  row, column, groupOn, groupOff, text, onColor, offColor, onlyOff):
        self.row = row
        self.column = column
        self.groupOn = groupOn
        self.groupOff = groupOff
        self.text = text
        self.onColor = onColor
        self.offColor = offColor
        self.onlyOff = onlyOff

def setButtonInfo():
    # light(text, row, column, Grupp Tänd, Grupp släck, onColor, offColor, onlyOff)
    myButton.append(buttonFunction(0,0,801,701,"Plan 12\nMaskinrum",onWorkLight, offWorkLight, 1))
    myButton.append(buttonFunction(1,0,802,702,"Plan 12\nTågvind",onWorkLight, offWorkLight, 0))
    myButton.append(buttonFunction(2,0,801,701,"Plan 11\nBalkong",onWorkLight, offWorkLight, 0))
    myButton.append(buttonFunction(3,0,801,701,"Plan 10\nBalkong",onWorkLight, offWorkLight, 0))
    myButton.append(buttonFunction(4,0,801,701,"Plan 9\nBalkong",onWorkLight, offWorkLight, 0))
    myButton.append(buttonFunction(5,0,801,701,"Scen",onWorkLight, offWorkLight, 0))
    myButton.append(buttonFunction(6,0,801,701,"Scenkällare",onWorkLight, offWorkLight, 0))

# Colorvariable for buttons
markedLight = "#ff4040"
onWorkLight = "#FFFFE0"
offWorkLight = "#FF9030"
onBlueLight = "#E0FFFF"
offBlueLight = "#3090FF"

# Button-array
buttons = []

# Function-array
myButton = []

# STARTUP ORDER
setButtonInfo()

# Add button to table
for i in range(0, len(myButton)):
    buttons.append(None)
    buttons[i] = Button(tab1,
    text = myButton[i].text,
        command = lambda c = i: do_something(c),
        font="Helvetica, 12",
        bg=myButton[i].offColor,
        width="10", height="2",
        highlightthickness="2",
        highlightbackground=myButton[i].offColor ).place(x=int(myButton[i].row*100+200), y=int(myButton[i].column*100+200))

# When button is puched
def do_something(i):
    button = buttons[int(i)]
    if button['relief'] == 'raised' :
        if int(myButton[i].onlyOff) < 1 :
            button.configure(relief='sunken', bg=markedLight)
    else:
        button.configure(relief='raised', bg=myButton[i].offColor)


root = Root()
root.mainloop()
Reply


Messages In This Thread
Found buttonstate in tabs - by MacTommu - Sep-22-2021, 11:10 AM
RE: Found buttonstate in tabs - by MacTommu - Sep-22-2021, 01:47 PM
RE: Found buttonstate in tabs - by deanhystad - Sep-22-2021, 04:10 PM
RE: Found buttonstate in tabs - by MacTommu - Sep-22-2021, 05:39 PM
RE: Found buttonstate in tabs - by deanhystad - Sep-22-2021, 05:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TabError: inconsistent use of tabs and spaces in indentation hobbyist 3 4,932 Jun-15-2021, 10:38 PM
Last Post: davide73_italy
  [Tkinter] Vertical Tabs Alignment in Tkinter muhammadasim 2 6,050 Oct-05-2020, 08:40 AM
Last Post: Larz60+
  [Tkinter] Need help please properly putting tabs within a PanedWindow JackMack118 2 3,381 Dec-08-2019, 03:02 PM
Last Post: balenaucigasa
  [PyQt] Drag items across tabs Alfalfa 5 4,808 Sep-01-2019, 11:58 PM
Last Post: Alfalfa
  [Tkinter] Adding space between Notebook tabs Columbo 4 4,568 Jul-10-2019, 10:46 PM
Last Post: Columbo
  [Tkinter] How to get a tabs works exactly same as google chrome sarthak260 0 3,801 Mar-07-2019, 10:45 AM
Last Post: sarthak260
  [Tkinter] How to create multilple tabs in tkinter from different classes Rishav 5 18,349 Jul-11-2018, 11:59 AM
Last Post: CCChris91
  How to create mutiple tabs in tkinter using oops Rishav 2 6,898 Jul-12-2017, 04:43 PM
Last Post: Rishav

Forum Jump:

User Panel Messages

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