Reputation:
i wrote this code, but when i run it it doesnt show any error, but the app is blank can you please show where i have done the mistake. Thank You
from tkinter import *
import csv
import os
os.chdir(r"C:\Users\Umer Selmani\Desktop\prog.practice\MP1")
root=Tk()
class Diet:
def __init__(self,Tops,Lefts, Rights):
self.Tops=Frame(root,width= 200,height=200).pack(side=TOP)
self.Lefts=Frame(root,width= 200,height=200).pack(side=LEFT)
self.Rights=Frame(root,width= 200,height=200).pack(side=RIGHT)
self.label1=Label(Tops,font=("ariel","bold" ,20),text="Sehir Cafeteria",
bg="darkblue").grid(row=0,columns=0)
root.mainloop()
Upvotes: 0
Views: 48
Reputation: 7176
You don't need to pass Tops, Lefts & Rights to the __init__()
function if they are the frames you want to display. They are created in __init__()
.
The pack()
function returns None
which means you don't keep a reference to the respective frames. I have corrected this in my example.
To display the frames defined in the Diet
class you must first create an instance.
I have color-coded the frames so they are visible.
from tkinter import *
class Diet:
def __init__(self):
self.Tops = Frame(root, width=400, height=200, bg='tan')
self.Tops.pack(side=TOP)
self.Tops.pack_propagate(False)
self.Lefts = Frame(root, width= 200, height=200, bg='salmon')
self.Lefts.pack(side=LEFT)
self.Rights = Frame(root, width= 200, height=200, bg='bisque')
self.Rights.pack(side=RIGHT)
self.label1 = Label(self.Tops, font="ariel 20 bold",
text="Sehir Cafeteria", bg="darkblue")
self.label1.pack(fill='x')
# Grid() will shrink the self.Tops frame to exactly
# fit the label.
root = Tk()
diet = Diet() # Create an instance of Diet.
root.mainloop()
Additional explanation
Different return values from widget creation depending on how the code is written depends on how information is passed between objects:
# The first code:
self.Tops = Frame(root, width=400, height=200, bg='tan')
# Creates a Frame and associates it with the name "self.Tops"
self.Tops.pack(side=TOP)
# uses the geometry manager pack to display the frame "self.Tops"
# The method call returns the value: None
# self.Tops is still referencing the frame
# The second code does the same thing but in a single compound statement
self.Tops = Frame(root, width=400, height=200, bg='tan').pack(side=TOP)
# Creates a frame and passes a reference to that frame to pack.
# Pack displays the label on the window and returns the value: None
# This value is given the reference "self.Tops"
Try running the code below and examine the console printout.
from tkinter import *
root = Tk()
label_a = Label(root, text='A')
return_a = label_a.pack()
print('label_a:', label_a)
print('return_a:', return_a)
label_b = Label(root, text='B').pack()
print('label_b:', label_b)
root.mainloop()
Hope this makes things clearer.
Upvotes: 0