Sep-22-2021, 11:10 AM
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:
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:
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()
