0
from tkinter import *

# ==================================================Settings=======================================================
root = Tk()
root.title("Video Youtube Downloader") # set up the title and size.
root.configure(background='black') # set up background.
root.minsize(800, 500)
root.maxsize(800, 500)
# ==================================================Frames=======================================================
top = Frame(root, width=800, height=50,  bg='yellow').pack(side=TOP)
bottom = Frame(root, width=800, height=50,  bg='red').pack(side=BOTTOM)
left = Frame(root,  width=550, height=450, bg='black').pack(side=LEFT)
right = Frame(root, width=250, height=450, bg='blue').pack(side=RIGHT)



# ==================================================Buttons=======================================================
btn_clear_url = Button(right, text="Clear Url", font=('arial', 10, 'bold')).grid(row=1, columns=1)

I am trying to add buttons to the right Frame, but for some reason when I run the program the IDE shows that it is running but there is no window. When I delete .grid(row=1, columns=1) the window appears. How can I fix this bug, and add btn_clear_url button to the right Frame?

adder
  • 3,512
  • 1
  • 16
  • 28
Mohammad Mahjoub
  • 417
  • 4
  • 12
  • Please show a [mcve]. There is nothing more n the code you posted that would cause the behaviou you describe. – Bryan Oakley Oct 16 '17 at 00:21
  • I think the bug as @mentalita pointed out is that I am using ".pack(side=TOP)" in the same line. Thus, when adding ".grid(row=1, columns=1)" at the same time the window is not loading. – Mohammad Mahjoub Oct 16 '17 at 04:26

3 Answers3

2
  • First of all, you need to invoke Tk's mainloop at the end of your code (you can see here why).
  • Another problem is chaining method calls like that. You're actually assigning return value of the last call to the variable, which is None in case of grid() and pack() - therefore, all your variables end up having None as the value. You need to separate widget instantiating call and grid or pack call and put each on its own line.
  • Other than that, you're setting both minsize and maxsize to the very same size - if you're really just trying to make your window not resizable, set the size with:

    root.geometry('800x500') # for instance
    

    ...and after that configure resizable attribute:

    root.resizable(width=False, height=False) 
    
  • Also, I suggest you get rid of from tkinter import *, since you don't know what names that imports. It can replace names you imported earlier, and it makes it very difficult to tell where names in your program are supposed to come from. Use import tkinter as tk instead.
adder
  • 3,512
  • 1
  • 16
  • 28
0

When you place widget in a tk window you cannot use grid and pack at the same time in the same window, so you use should use pack () instead of grid()

samuel
  • 21
  • 4
0

The problem starts with this line of code:

right = Frame(root, width=250, height=450, bg='blue').pack(side=RIGHT)

With that, right is set to None because .pack(side=RIGHT) returns None.

Next, you do this:

btn_clear_url = Button(right, ...)).grid(row=1, columns=1)

Because right is None, it's the same as Button(root, ...). Since you are already using pack for a widget in root, you can't also use grid.

The solution -- and best practice -- is to separate widget creation from widget layout:

top = Frame(root, width=800, height=50,  bg='yellow')
bottom = Frame(root, width=800, height=50,  bg='red')
left = Frame(root,  width=550, height=450, bg='black')
right = Frame(root, width=250, height=450, bg='blue')

top.pack(side=TOP)
bottom.pack(side=BOTTOM)
left.pack(side=LEFT)
right.pack(side=RIGHT)

With the above, right will be properly set to the frame instance, and adding a widget to right and using grid will no longer fail.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685