-1

So I started creating GUI that I'll use for testbench for electrical engines. My question is why is everything displayed in master window like this?:

everything displayed

Instead of the two frames?

The first time I wrote it, it was working perfectly:

[first time[2]

but then I wanted to rearrange the code using classes and the problem appeared. I'm using Python 3.7.

My code:

import tkinter as tk
from tkinter import *
from tkinter import messagebox

class Lintebench():
    def __init__(self, master):
        self.master = master
        self.master.title("Linte^2 testbench")
        self.master.geometry('1000x600')

    #FRAMES
        frame = Frame(master, bg='#3e646c').place(relwidth=0.2, relheight=0.4, rely=0.6)
        frame2 = Frame(master, bg='#3e646c').place(relwidth=0.8, relheight=0.4, rely=0.6, relx=0.2)

    #FRAME 1
        self.start_button = Button(frame, text="Start", padx=50, pady=50, bg='green', activebackground='grey', command=self.start_engine).pack()
        self.stop_button = Button(frame2, text="Stop", padx=50, pady=50, bg='red', activebackground='grey', command=self.stop_engine).pack()

    #FRAME 2
        self.parameters = Label(frame2, text="PARAMETERS", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0)
        self.set_parameter = Label(frame2, text="SET", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0.8)
        self.values = Label(frame2, text="VALUES", font=("Arial", 16), fg='white', bg='#3e646c').place(relx=0.4)
        self.torque = Label(frame2, text="TORQUE", font=("Arial", 12), fg='white', bg='#3e646c').place(relx=0.7, rely=0.2)
        self.velocity = Label(frame2, text="VELOCITY", font=("Arial", 12), fg='white', bg='#3e646c').place(relx=0.7, rely=0.6)

    #parameters to set
        self.set_torque = Scale(frame2, orient=HORIZONTAL, length=200).place(relx=0.7, rely=0.3)
        self.set_velocity = Scale(frame2, orient=HORIZONTAL, length=200).place(relx=0.7, rely=0.7)

    def start_engine(*args):
        messagebox.showinfo('Information','Engine was started')

    def stop_engine(*args):
        messagebox.showinfo('Information','Engine was stopped')

def main():
    root = Tk()
    lintebench = Lintebench(root)
    root.mainloop()

if __name__ == '__main__':
    main()here
martineau
  • 119,623
  • 25
  • 170
  • 301
ogtom
  • 11
  • 4
  • 1
    Please provide your code in the question as text. Don't use screenshots or links to other sites hosting your code. – dspencer Apr 10 '20 at 17:16
  • 1
    Have you looked at the values of your frame variables to see if they are what you think they are? – Bryan Oakley Apr 10 '20 at 17:36
  • 1
    Read up on [tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name](https://stackoverflow.com/questions/1101750) – stovfl Apr 10 '20 at 17:55
  • It is because `frame` and `frame2` are None because they are assigned the result of `place(...)`, not `Frame(...)`. You have chained `Frame(...)` with `place(...)`. Separate them into two statements instead. – acw1668 Apr 11 '20 at 01:04

2 Answers2

1

It is because you have chained Frame(...) with place(...) and so frame and frame2 will be None. Separate the chained statement into two statements:

frame = Frame(master, bg='#3e646c')
frame.place(relwidth=0.2, relheight=0.4, rely=0.6)
frame2 = Frame(master, bg='#3e646c')
frame2.place(relwidth=0.8, relheight=0.4, rely=0.6, relx=0.2)

Actually you need to separate all the chained statements in your code if you want to reference the widgets in other place.

Also self.stop_button should be child of frame, not frame2:

self.stop_button = Button(frame, text="Stop", padx=50, pady=50, bg='red', activebackground='grey', command=self.stop_engine)
self.stop_button.pack()
acw1668
  • 40,144
  • 5
  • 22
  • 34
-1

I believe is becuase you're asigning their master as root, and you only have one root and when you make roo.mainloop() that makes them focus.