Python Forum
Found buttonstate in tabs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Found buttonstate in tabs
#3
When I run this:
import tkinter as tk

root = tk.Tk()
b1 = tk.Button(text='Button 1').pack()
b2 = tk.Button(text='Button 2')
b2.pack()
print('b1', b1)
print('b2', b2)
root.mainloop()
it prints:
Output:
b1 None b2 .!button2
b1 is None because .pack() (and .place() and .grid()) is a function that returns None. So when you create buttons like this:
    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))
You are putting None in myButton[i].

What you report as a fix does nothing to fix your code. if your code is working now you must have split up making your buttons and placing your buttons.

As an aside, you should avoid using .place(). Using .place() results in windows that cannot be resized and layouts that are difficult to modify.
Learn how the geometry managers work (.pack() and .grid()).

As another aside, this makes no sense at all:
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))
Something like this makes sense if you are using a designer that creates a configuration file that you load into program to make the GUI. In your code it only creates extra (and confusing) variables and steps. if you want to add information to a button you should make your own button class. In this example I add your on and off colors and group information to tk.Button.
from tkinter import ttk
import tkinter as tk

# Colors for buttons
MARKEDLIGHT  = "#ff4040"
ONWORKLIGHT  = "#FFFFE0"
OFFWORKLIGHT = "#FF9030"
ONBLUELIGHT  = "#E0FFFF"
OFFBLUELIGHT = "#3090FF"

class MyTab(tk.Frame):
    def __init__(self, parent, text):
        super().__init__(parent)
        parent.add(self, text=text)

class MyButton(tk.Button):
    def __init__(
            self,
            parent,
            text='',
            row=0,
            column=0,
            groupOn=801,
            groupOff=701,
            onColor=ONWORKLIGHT,
            offColor=OFFWORKLIGHT,
            onlyOff=False,
            **kwargs):
        super().__init__(
            parent,
            text=text,
            width=10,
            height=2,
            bg=offColor,
            highlightthickness=2,
            highlightbackground=offColor,
            command=self.toggle_relief,
            **kwargs)
        self.place(x=row*100+200, y=column*100+200)
        self.onColor = onColor
        self.offColor = offColor
        self.groupOn = groupOn
        self.groupOff = groupOff
        self.onlyOff = onlyOff

    def toggle_relief(self):
        if self['relief'] != 'raised':
            self.configure(relief='raised', bg=self.offColor)
        elif not self.onlyOff:
            self.configure(relief='sunken', bg=MARKEDLIGHT)

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') )
tabs = [
    MyTab(tabControl, ' Start '),
    MyTab(tabControl, ' Arbetsljus '),
    MyTab(tabControl, ' Blåljus '),
    MyTab(tabControl, ' Övrigt ')
]
tabControl.pack(expand = 1, fill ="both")

MyButton(tabs[0], "Plan 12\nMaskinrum", row=0)
MyButton(tabs[0], "Plan 12\nTågvind", row=1, groupOn=802, groupOff=702)
MyButton(tabs[0], "Plan 11\nBalkong", row=2)
MyButton(tabs[0], "Plan 10\nBalkong", row=3)
MyButton(tabs[0], "Plan 9\nBalkong", row=4)
MyButton(tabs[0], "Scen", row=5)
MyButton(tabs[0], "Scenkällare", row=6)

root.mainloop()
I also made my own tab class that makes tabs a little easier to work with.

If you want to write Python GUI code you should start with learning about classes and inheritance.
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 5,023 Jun-15-2021, 10:38 PM
Last Post: davide73_italy
  [Tkinter] Vertical Tabs Alignment in Tkinter muhammadasim 2 6,253 Oct-05-2020, 08:40 AM
Last Post: Larz60+
  [Tkinter] Need help please properly putting tabs within a PanedWindow JackMack118 2 3,461 Dec-08-2019, 03:02 PM
Last Post: balenaucigasa
  [PyQt] Drag items across tabs Alfalfa 5 4,928 Sep-01-2019, 11:58 PM
Last Post: Alfalfa
  [Tkinter] Adding space between Notebook tabs Columbo 4 4,674 Jul-10-2019, 10:46 PM
Last Post: Columbo
  [Tkinter] How to get a tabs works exactly same as google chrome sarthak260 0 3,874 Mar-07-2019, 10:45 AM
Last Post: sarthak260
  [Tkinter] How to create multilple tabs in tkinter from different classes Rishav 5 18,513 Jul-11-2018, 11:59 AM
Last Post: CCChris91
  How to create mutiple tabs in tkinter using oops Rishav 2 6,969 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