Sep-22-2021, 04:10 PM
(This post was last modified: Sep-22-2021, 04:10 PM by deanhystad.)
When I run this:
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:
If you want to write Python GUI code you should start with learning about classes and inheritance.
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.