Python Tkinter: Display description in status bar - python

For my Tkinter GUI, I have already created a Menu and a Status Bar. However, how to display description when mouse is over menu items?
Please run the following code (eg. when mouse is over menu item "About", status bar should write "Information":
from Tkinter import Tk, Frame, BOTH, Menu, Label, SUNKEN, X, BOTTOM
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background = "white")
parent.configure(bg = "black")
self.pack(fill = BOTH, expand = True, padx = 20, pady = 20)
self.parent = parent
# Maximize window
self.screenWidth = self.parent.winfo_screenwidth() - 5
self.screenHeight = self.parent.winfo_screenheight() - 110
self.parent.geometry('%dx%d+%d+%d' % (self.screenWidth, self.screenHeight, 0, 0))
self.parent.resizable(0, 0)
# Status bar
self.statusBar = StatusBar(self.parent)
self.statusBar.pack(side = BOTTOM, fill = X)
# Menu bar
menubar = Menu(self.parent)
self.parent.config(menu = menubar)
self.commandMenu = Menu(menubar, tearoff = 0)
self.commandMenu.add_command(label = "Rename", command = self.onRename)
menubar.add_cascade(label = "Command", menu = self.commandMenu)
self.helpMenu = Menu(menubar, tearoff = 0)
self.helpMenu.add_command(label = "About", command = self.onAbout)
menubar.add_cascade(label = "Help", menu = self.helpMenu)
def onRename(self):
pass
def onAbout(self):
pass
class StatusBar(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.label = Label(self, bd = 1, relief = SUNKEN, anchor = "w")
self.label.pack(fill=X)
def set(self, format0, *args):
self.label.config(text = format0 % args)
self.label.update_idletasks()
def clear(self):
self.label.config(text="")
self.label.update_idletasks()
def main():
root = Tk()
Application(root)
root.mainloop()
if __name__ == '__main__':
main()

So, the thing you're looking for is the <<MenuSelect>>.
If you bind the self.helpMenu to <<MenuSelect>>, like in my update of your code:
__author__ = 'rcope'
from Tkinter import Tk, Frame, BOTH, Menu, Label, SUNKEN, X, BOTTOM
class Application(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background = "white")
parent.configure(bg = "black")
self.pack(fill = BOTH, expand = True, padx = 20, pady = 20)
self.parent = parent
# Maximize window
self.screenWidth = self.parent.winfo_screenwidth() - 5
self.screenHeight = self.parent.winfo_screenheight() - 110
self.parent.geometry('%dx%d+%d+%d' % (self.screenWidth, self.screenHeight, 0, 0))
self.parent.resizable(0, 0)
# Status bar
self.statusBar = StatusBar(self.parent)
self.statusBar.pack(side = BOTTOM, fill = X)
# Menu bar
menubar = Menu(self.parent)
self.parent.config(menu = menubar)
self.commandMenu = Menu(menubar, tearoff = 0)
self.commandMenu.add_command(label = "Rename", command = self.onRename)
menubar.add_cascade(label = "Command", menu = self.commandMenu)
self.helpMenu = Menu(menubar, tearoff = 0)
self.helpMenu.add_command(label = "About", command = self.onAbout)
menubar.add_cascade(label = "Help", menu = self.helpMenu)
self.helpMenu.bind("<<MenuSelect>>", self.statusBarUpdate)
def onRename(self):
pass
def onAbout(self):
pass
def statusBarUpdate(self, event=None):
print "Status Bar Update Called"
if self.parent.call(event.widget, "index", "active") == 0:
self.statusBar.set("About This Application")
else:
self.statusBar.set("")
class StatusBar(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.label = Label(self, bd = 1, relief = SUNKEN, anchor = "w")
self.label.pack(fill=X)
def set(self, format0, *args):
self.label.config(text = format0 % args)
self.label.update_idletasks()
def clear(self):
self.label.config(text="")
self.label.update_idletasks()
def main():
root = Tk()
Application(root)
root.mainloop()
if __name__ == '__main__':
main()
The only thing you need to keep track of now, is what index corresponds to what menu item. I would recommend keeping an int you increment each time you add a menu entry, and using each index via a dict to get whatever relevant information about the menu item you need. Tkinter doesn't tell you a lot through the entry in the statusBarUpdate callback (like that this was called from the menu command labeled "About"), so you need to roll this yourself, I think.

Related

tkinter.place() not working and window still blank

I have a problem with tkinter.place, why it is not working?
class KafeDaun(tk.Frame):
def __init__(self, master = None):
super().__init__(master)
self.master.title("Kafe Daun-Daun Pacilkom v2.0 🌿")
self.master.geometry("500x300")
self.master.configure(bg="grey")
self.create_widgets()
self.pack()
def create_widgets(self):
self.btn_buat_pesanan = tk.Button(self, text = "Buat Pesanan", width = 20)
self.btn_buat_pesanan.place(x = 250, y = 100)
self.btn_meja = tk.Button(self, text = "Selesai Gunakan Meja", width = 20)
I still get this blank Frame even though already use tkinter.place on btn_buat_pesanan
I expect it to have a button on the exact location, like when using tkinter.pack() or tkinter.grid(). Do you have any suggestion
... ... ... ... ..
Try this.
You have to pack the frame like this self.pack(fill="both", expand=True). Because the place did not change the parent size, that's why it didn't visible
import tkinter as tk
class KafeDaun(tk.Frame):
def __init__(self, master = None):
super().__init__(master)
self.master.title("Kafe Daun-Daun Pacilkom v2.0 🌿")
self.master.geometry("500x300")
self.master.configure(bg="grey")
self.create_widgets()
self.pack(fill="both", expand=True)
def create_widgets(self):
self.btn_buat_pesanan = tk.Button(self, text = "Buat Pesanan", width = 20)
self.btn_buat_pesanan.place(x = 250, y = 100)
self.btn_meja = tk.Button(self, text = "Selesai Gunakan Meja", width = 20)
app =tk.Tk()
s = KafeDaun(app)
app.mainloop()
Or you can set the width and height of the frame. super().__init__(master, width=<width>, height=<height>)

How can I detect which on which frame was a Button Clicked in Tkinter?

I have a question. I have this code:
import tkinter as tk
class new_f:
def __init__(self,root,num):
self.new_frame=tk.Frame(root,width=100,height=100,bg='white',bd=3,relief=tk.GROOVE)
self.new_frame.pack(side=tk.LEFT,fill=tk.X,expand=True)
self.num=num
def add_label(self,t):
self.l1=tk.Label(self.new_frame,bg='white',text=t)
self.l1.pack()
def return_instance(self):
return self.num
class Main_win:
def __init__(self,root):
self.root=root
self.bind_number=0
self.current_index=0
self.instance_list=[]
self.b1=tk.Button(self.root,text='Add Frame',command=self.add_frame_win)
self.b1.pack(side=tk.BOTTOM)
self.b2=tk.Button(self.root,text='Add text',command=self.add_text_frame)
self.b2.pack(side=tk.BOTTOM)
def return_instance_num(self,num,*args):
self.current_index=num
def add_frame_win(self):
new_in=new_f(self.root,self.bind_number)
self.instance_list.append(new_in)
new_in.new_frame.bind('<Button-1>',lambda evnt: self.return_instance_num(new_in.return_instance()))
#self.current_index=new_in.return_instance()
self.bind_number+=1
def add_text_frame(self):
instance=self.instance_list[self.current_index]
instance.add_label('Hello World')
root=tk.Tk()
ob=Main_win(root)
root.mainloop()
What I a trying to achieve is that I want to detect on which frame was the left mouse-button clicked so as to make that Frame active and add the labels to that particular Frame. However, I am stuck on how would I go about writing the code. I need a new class Because I don't know how many frames will the user need.
This is a short example of the code I will be implementing later. So my question is:
How will I go to detect which frame was picked so as to make it active to add the labels?
In this approach I have label l1 bound to Button-1
This was achieved by passing self to new_f instead of root
and binding self.l1 to Button-1
import tkinter as tk
class new_f:
def __init__(self, prog, num):
self.prog = prog
self.new_frame = tk.Frame(prog.root, width = 100, height = 100, bg = 'white', bd = 3, relief = tk.GROOVE)
self.new_frame.pack(side = tk.LEFT, fill = tk.X, expand = True)
self.num = num
def add_label(self, t):
self.l1 = tk.Label(self.new_frame, bg = 'white', text = t)
self.l1.pack()
# binding button-1 press to label
self.l1.bind("<Button-1>", lambda evnt: self.prog.return_instance_num(self.return_instance()))
def return_instance(self):
return self.num
class Main_win:
def __init__(self, root):
self.root = root
self.bind_number = 0
self.current_index = 0
self.instance_list = []
self.b1 = tk.Button(self.root, text = 'Add Frame', command = self.add_frame_win)
self.b1.pack(side = tk.BOTTOM)
self.b2 = tk.Button(self.root, text = 'Add text', command = self.add_text_frame)
self.b2.pack(side = tk.BOTTOM)
def return_instance_num(self, num, *args):
self.current_index = num
def add_frame_win(self):
# note passing self not root
new_in = new_f(self, self.bind_number)
self.instance_list.append(new_in)
new_in.new_frame.bind('<Button-1>', lambda evnt: self.return_instance_num(new_in.return_instance()))
#self.current_index = new_in.return_instance()
self.bind_number = self.bind_number + 1
def add_text_frame(self):
instance = self.instance_list[self.current_index]
instance.add_label('Hello World')
root = tk.Tk()
ob = Main_win(root)
# This necessary to prevent error if user hits 'Add text' before 'Add Frame'
ob.add_frame_win()
root.mainloop()
Here is an alternative method that uses dictionaries to store l1 and new_frame objects as keys and new_f instances as values.
This method can be used for other tkinter objects (Entry, Listbox, Text, Canvas)
import tkinter as tk
class new_f:
def __init__(self, parent):
self.parent = parent
self.frame = tk.Frame(
parent.root, width = 100, height = 100,
bg = "white", bd = 3, relief = tk.GROOVE)
self.frame.pack(
side = tk.LEFT, fill = tk.X, expand = True)
self.frame.bind("<Button-1>", parent.get_current_frame)
def add_label(self, t):
self.label = tk.Label(self.frame, bg = "white", text = t)
self.label.pack(fill = tk.BOTH, expand = True)
# bind button-1 to label, set instance_label and current to self
self.label.bind("<Button-1>", self.parent.get_current_label)
self.parent.instance_label[self.label] = self.parent.current = self
class Main_win:
instance_label = dict() # This method can be expanded for other objects
instance_frame = dict() # that you may want to create in frames
def __init__(self, root):
self.root = root
self.b1 = tk.Button(
self.root, text = "Add Frame", command = self.add_frame_win)
self.b1.pack(side = tk.BOTTOM)
self.b2 = tk.Button(
self.root, text = "Add text", command = self.add_text_frame)
self.b2.pack(side = tk.BOTTOM)
def get_current_label(self, ev):
self.current = self.instance_label[ev.widget]
def get_current_frame(self, ev):
self.current = self.instance_frame[ev.widget]
def add_frame_win(self):
# note passing self not root
self.new_in = new_f(self)
self.instance_frame[self.new_in.frame] = self.current = self.new_in
def add_text_frame(self):
# Change message with entry tool?
self.current.add_label("Hello World")
root = tk.Tk()
ob = Main_win(root)
# This necessary to prevent error if user hits 'Add text' before 'Add Frame'
ob.add_frame_win()
root.mainloop()

AttributeError: 'Application' object has no attribute 'create_widgets'

I have a little problem with my code. I have to make a sumator but I have an error and I don't know where I have a mistake. It says
"AttributeError: 'Application' object has no attribute 'create_widgets'.
I would be grateful if you help me fix the problem!
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master = None):
super().__init__(master)
self.pack()
self.create_widgets()
#create the app
app = Application()
app.master.frame()
app.master.title("Sumator")
app.master.minsize(width=100, height=50)
#start the program
app.mainloop()
def create_widgets(self):
#create widgets
self.firstNumberEntry = tk.Entry()
self.plusSign = tk.Label(text = "+")
self.secondNumberEntry = tk.Entry()
self.equalSign = tk.Label(text = "=")
self.resultLabel = tk.Label(text = "Result...", bg = "green", fg = "white")
self.calculateButton = tk.Button(text = "Calculate", command = self.calculate)
#place widgets
self.firstNumberEntry.pack(side = "left")
self.plusSign.pack(side = "left")
self.secondNumberEntry.pack(side = "left")
self.equalSign.pack(side = "left")
self.resultLabel.pack(side = "left")
self.calculateButton.pack(side = "left")
def calculate(self):
try:
first_value = float(self.firstNumberEntry.get())
second_value = float(self.secondNumberEntry.get())
result = first_value + second_value
self.resultLabel.config(text = str(result), bg = "green", fg = "white")
except ValueError:
self.resultLabel.config(text="No number/s", bg="red", fg="black")```
Your methods create_widgets and calculate are not in your class actually, they should be indented like the __init__ , like this
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master = None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
#...
def calculate(self):
#...
#create the app
app = Application()
app.master.frame()
app.master.title("Sumator")
app.master.minsize(width=100, height=50)
#start the program
app.mainloop()
You need to indent the calculate(self) function and the create_widgets(self) function into your Application class. This way, python knows they are part of the class.
I also made several improvements to your program, such as making it look nicer.
Here is the final code:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master = None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
#create widgets
self.firstNumberEntry = tk.Entry(bd = 0)
self.plusSign = tk.Label(text = "+")
self.secondNumberEntry = tk.Entry(bd = 0)
self.equalSign = tk.Label(text = "=")
self.resultLabel = tk.Label(text = "Result...", bg = "green", fg = "white")
self.calculateButton = tk.Button(text = "Calculate", command = self.calculate, bd = 0, relief = "flat")
self.calculateButton.config(activebackground = self.calculateButton.master.cget("bg"))
#place widgets
self.firstNumberEntry.pack(side = "left")
self.plusSign.pack(side = "left")
self.secondNumberEntry.pack(side = "left")
self.equalSign.pack(side = "left")
self.resultLabel.pack(side = "left")
self.calculateButton.pack(side = "left")
def calculate(self):
try:
first_value = float(self.firstNumberEntry.get())
second_value = float(self.secondNumberEntry.get())
result = first_value + second_value
self.resultLabel.config(text = str(result), bg = "green", fg = "white")
except ValueError:
self.resultLabel.config(text="No number/s", bg="red", fg="black")
#create the app
app = Application()
app.master.frame()
app.master.title("Sumator")
app.master.minsize(width=100, height=50)
#start the program
app.mainloop()
Hope this helps you!

having trouble in setting up a calendar in tkinter

i have a small ui programm and i need to display a calendar in it or a date picker . (NOTE : userid and password is root")**
i have tried this code :
from Tkinter import *
from PIL import Image, ImageTk
import ttkcalendar
class App():
def __init__(self):
pass
root = Tk()
root.configure(bg='black')
root.attributes('-alpha', 0.0)
def mainWindow(self):
self.root.geometry('{}x{}'.format(600,400))
self.root.attributes('-alpha', 1)
self.root.configure(bg='#404040')
self.ttkcal = ttkcalendar.Calendar(self.root,firstweekday=calendar.SUNDAY)
self.ttkcal.pack()
self.root.wm_title("time sheet management system")
# Create the toolbar as a frame
self.toolbar = Frame(self.root, borderwidth=1, relief='raised')
self.toolbar.configure( bg = '#838383')
# Load all the images first as PNGs and use ImageTk to convert
# them to usable Tkinter images.
self.img1 = Image.open('NewIcon.png')
self.useImg1 = ImageTk.PhotoImage(self.img1)
self.img2 = Image.open('LoadIcon.png')
self.useImg2 = ImageTk.PhotoImage(self.img2)
self.img3 = Image.open('SaveIcon.png')
self.useImg3 = ImageTk.PhotoImage(self.img3)
# Set up all the buttons for use on the toolbars.
newBtn = Button(self.toolbar, image=self.useImg1, command=self.callback)
newBtn.pack(side=LEFT, fill=X)
loadBtn = Button(self.toolbar, image=self.useImg2, command=self.callback)
loadBtn.pack(side=LEFT, fill=X)
saveBtn = Button(self.toolbar, image=self.useImg3, command=self.callback)
saveBtn.pack(side=LEFT, fill=X)
# Add the toolbar
self.toolbar.pack(side=TOP, fill=X)
"""
#create a frame
self.infoArea= Frame(self.root, borderwidth=2,height=40,width=100, relief='raised',bg='red')"""
self.root.mainloop()
"""
# Set up a Text box and scroll bar.
self.scrollbar = Scrollbar(self.root)
self.scrollbar.pack(side=RIGHT, fill=Y)
self.text = Text(self.root)
self.text.pack()
self.text.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.text.yview)"""
def loginClick(self):
self.userid = self.txt1.get()
self.password = self.txt2.get()
print self.userid,self.password
if self.password == 'root' and self.userid == 'root':
self.wi.destroy()
self.mainWindow()
def callback(self):
print "A button was pressed"
def login(self):
self.wi = Toplevel (self.root)
#self.wi.geometry('{}x{}'.format(200,200))
self.wi.configure(bg='#404040')
self.wi.overrideredirect(1)
self.wi.update_idletasks()
self.w = self.wi.winfo_screenwidth()
self.h = self.wi.winfo_screenheight()
self.size = tuple(int(_) for _ in self.wi.geometry().split('+')[0].split('x'))
self.x = self.w/2 - self.size[0]/2
self.y = self.h/2 - self.size[1]/2
self.wi.geometry("%dx%d+%d+%d" % (self.size + (self.x, self.y)))
self.wi.geometry('{}x{}'.format(400,200))
self.frame1 = Frame ( self.wi,bg='#404040' )
self.frame1.pack(side=BOTTOM,fill=X)
self.butt1 = Button(self.frame1,text= "Cancel" , bg = '#838383',fg='white',command = self.wi.destroy)
self.butt1.pack(side=RIGHT,padx=1)
self.butt2 = Button(self.frame1,text= "Login" ,bg = '#838383',fg='white',command = self.loginClick)
self.butt2.pack(side=RIGHT)
"""self.frame2 = Frame ( self.wi,bg='green' )
self.frame2.pack(side=BOTTOM,fill=BOTH)"""
self.lab1 = Label (self.wi,text='UserID',bg='#404040',fg='white')
#self.lab1.pack(side=LEFT,padx=60,pady=20)
self.lab1.place(x=60,y=20)
self.lab2 = Label (self.wi,text='Password',bg='#404040',fg='white')
#self.lab1.pack(side=LEFT,padx=60,pady=20)
self.lab2.place(x=60,y=60)
self.txt1 = Entry( self.wi)
self.txt1.place(x=140,y=20)
self.txt2 = Entry( self.wi,show='*')
self.txt2.place(x=140,y=60)
"""
self.label = Label(self.wi,text="UserID",bg='#838383',fg='white')
self.label.place("""
self.wi.mainloop()
if __name__ == "__main__":
a = App()
#a.root.mainloop()
a.login()
but it is giving error that "name calendar not defined". how can i solve this ?
or is there any other way to implement a calendar or date picker in tkinter.
The code is using calendar module, but it is not importing the module.
self.ttkcal = ttkcalendar.Calendar(self.root, firstweekday=calendar.SUNDAY)
^^^^^^^^^^^^^^^
Simply adding following import statement will solve the problem.
import calendar

Create n number of windows in tkinter

I am writing a programm in which I am trying to open a n number of windows. My code is:
from tkinter import *
from tkinter import ttk
class Main_window(ttk.Frame):
"""A program"""
def __init__(self, master):
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Creates all the objects in the window"""
self.min_lbl = ttk.Label(self, text = "1").grid(row = 0, column = 0,
sticky = W)
self.max_lbl = ttk.Label(self, text = "100").grid(row = 0, column = 2,
sticky = W)
spinval = IntVar()
self.scale = ttk.Scale(self, orient = HORIZONTAL,
length = 200,
from_ = 1, to = 100,
variable = spinval,
command=self.accept_whole_number_only)
self.scale.grid(row = 0,column = 1,sticky = W)
self.spinbox = Spinbox(self, from_ = 1, to = 100,
textvariable = spinval,
command = self.update,
width = 10)
self.spinbox.grid(row = 0,column =3,sticky = W)
self.go_bttn = ttk.Button(self, text = "Go",
command = self.create_windows
).grid(row = 1, column = 1, sticky = W)
def accept_whole_number_only(self, e=None):
"""Makes the numbers from the scale whole"""
value = self.scale.get()
if int(value) != value:
self.scale.set(round(value))
def update(self):
"""Updates the scale and spinbox"""
self.scale.set(self.spinbox.get())
def create_windows(self):
"""This function will create all the new windows"""
value = self.scale.get()
window_num = value
negative_window_num = 1
while window_num != 0:
root = Tk()
root.title("This is Window "+str(window_num)[:-2]+" of "+str(value)[:-2])
root.geometry("350x200")
app = Window_creator(root)
root.mainloop()
window_num -= 1
class Window_creator(ttk.Frame):
"""makes child windows"""
def __init__(self, master):
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""creates all the widgets in the window"""
def main():
"""Loops the window"""
root = Tk()
root.title("Programm")
root.geometry("350x200")
app = Main_window(root)
root.mainloop()
main()
What I want this code to do is I want to be able to set the spinbox or scale to number n and then when I click the Button i want n numbers of child windows to appear. I tried this with a while loop but it doesn't quite work like I want it to by creating a new window just after the I closed the prevoius window. You also have to close the main window first for it to work (I am going to make the button close the window automatically later). Any Ideas on how I could make this work?
Call child = Toplevel(), instead of root = Tk().
Also, you can not call mainloop more than once (since there should be only one event loop).
from tkinter import *
from tkinter import ttk
class Main_window(ttk.Frame):
"""A program"""
def __init__(self, master):
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Creates all the objects in the window"""
self.min_lbl = ttk.Label(self, text = "1").grid(row = 0, column = 0,
sticky = W)
self.max_lbl = ttk.Label(self, text = "100").grid(row = 0, column = 2,
sticky = W)
spinval = IntVar()
self.scale = ttk.Scale(self, orient = HORIZONTAL,
length = 200,
from_ = 1, to = 100,
variable = spinval,
command=self.accept_whole_number_only)
self.scale.grid(row = 0,column = 1,sticky = W)
self.spinbox = Spinbox(self, from_ = 1, to = 100,
textvariable = spinval,
command = self.update,
width = 10)
self.spinbox.grid(row = 0,column =3,sticky = W)
self.go_bttn = ttk.Button(self, text = "Go",
command = self.create_windows
).grid(row = 1, column = 1, sticky = W)
def accept_whole_number_only(self, e=None):
"""Makes the numbers from the scale whole"""
value = self.scale.get()
if int(value) != value:
self.scale.set(round(value))
def update(self):
"""Updates the scale and spinbox"""
self.scale.set(self.spinbox.get())
def create_windows(self):
"""This function will create all the new windows"""
value = self.scale.get()
window_num = value
negative_window_num = 1
for n in range(int(window_num)):
child = Toplevel()
child.title("This is Window "+str(window_num)[:-2]+" of "+str(value)[:-2])
child.geometry("350x200")
app = Window_creator(child)
class Window_creator(ttk.Frame):
"""makes child windows"""
def __init__(self, master):
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""creates all the widgets in the window"""
def main():
"""Loops the window"""
root = Tk()
root.title("Programm")
root.geometry("350x200")
app = Main_window(root)
root.mainloop()
main()

Categories