Reputation: 1
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename
class menu():
def __init__(self, master):
self.master = master
master.title('Library Management Software')
master.geometry('1230x650')
master.configure(background="#98ff98")
master.iconbitmap("e:\library\image\save.ico")#adding icon on title window
menubar = Menu(master)
membermenu = Menu(menubar, tearoff=0)
membermenu.add_command(label = "Member" , command = self.reg_window)
menubar.add_cascade(label = "Entry", menu = membermenu)
master.config(menu = menubar)
def reg_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = registration_window(self.newWindow)
class registration_window(Toplevel):
def __init__(self, master):
self.master=master
#self.transient(master)
master.title('Registration Form')
master.geometry('1230x650')
master.configure(background="#98ff98")
master.iconbitmap("e:\library\image\save.ico")#adding icon on title window
item_file = StringVar()
image_label = Label(master, text="Photo: ", bg="gray74", relief="groove")
image_label.place(x=755,y=150)
item_entry = Entry(master, textvariable=item_file,width=35)
item_entry.place(x=795,y=150)
self.item_button = Button(master, text="\uD83D\uDCC2", relief="groove",
command=lambda:entry_set(item_entry, askopenfilename()))
self.item_button.place(x=1000, y=150, height=20, width=20)
def entry_set(entry, text):
item_entry.delete(0, 'end')
item_entry.insert(END, text)
def main():
root = tk.Tk()
app = menu(root)
root.mainloop()
if __name__ == '__main__':
main()
Sir, I am learning tkinter for a couple of months and my problem is that i cannot understand where i will use this statement self.transient(master) so that when my the dialog box appear on the registration window where i will use transient statement in my code
Upvotes: 0
Views: 55
Reputation: 385980
The dialog function accepts a parent
option. From the documentation:
Makes window the logical parent of the file dialog. The file dialog is displayed on top of its parent window. On Mac OS X, this turns the file dialog into a sheet attached to the parent window.
Typically, in a class that inherits from Toplevel
you would use self
. However, you're not using inheritance correctly so that won't work without rewriting that class. So first, we need to rewrite that class.
The first thing to do is to call the __init__
method of the superclass. That is the secret sauce that makes your registration_window
an actual Toplevel
window.
class registration_window(Toplevel):
def __init__(self, master):
super().__init__(master)
...
Next, you need to remove all other uses of self.master
or master
and instead use self
so that all of the widgets are inside itself. In addition, add parent=self
when you call askopenfilename
. The class should look like this:
class registration_window(Toplevel):
def __init__(self, master):
super().__init__(master)
self.title('Registration Form')
self.geometry('1230x650')
self.configure(background="#98ff98")
self.iconbitmap("e:\library\image\save.ico")#adding icon on title window
item_file = StringVar()
image_label = Label(self, text="Photo: ", bg="gray74", relief="groove")
image_label.place(x=755,y=150)
item_entry = Entry(self, textvariable=item_file,width=35)
item_entry.place(x=795,y=150)
self.item_button = Button(self, text="yes, this button", relief="groove",
command=lambda:entry_set(item_entry, askopenfilename(parent=self)))
self.item_button.place(x=1000, y=150, height=20, width=100)
def entry_set(entry, text):
item_entry.delete(0, 'end')
item_entry.insert(END, text)
Finally, to use this class you don't need to create another Toplevel
. Change reg_window
to look like this:
def reg_window(self):
self.app = registration_window(self.master)
Upvotes: 1