I have little python programs to create html, for example insertMedia.py: this takes the name of the media file, puts it in the html code string and writes it to the output file.
insertMedia.py uses tkinter. I have a window with the buttons "insert audio" and 'insert video".
insertMedia.py works well, no problems. Also, I have some more "makehtml" programs, each for a specific task.
I want to have a master window, with buttons, or maybe a menu item for each "makehtml" program.
How can I open and run insertMedia.py from a master window?
Should I put all the "makehtml" programs in a module and import the module?
This code from Bryan Oakley, here in stackoverflow, opens a new window. Can it be modified to open my insertMedia.py?
import Tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()
To show you just the priciple of a common way to do this.
Frist you create a main file with your stuff in main.py wich looks like for exampel:
main.py
import tkinter as tk
import Mod1
root = tk.Tk()
def callback():
m1 = Mod1.Model(root, var='my_html')
b = tk.Button(text='click me', command=callback)
b.grid(column=0,row=1)
root.mainloop()
Then you are creating another script with your Toplevel and stuff in it like this:
Mod1.py
from __main__ import tk
class Model(tk.Toplevel):
def __init__(self, master, var=None):
tk.Toplevel.__init__(self, master)
self.master = master
self.configure(bg="red", width=300, height=300)
b=tk.Button(self,text='print html', command=lambda:print(var))
b.pack()
So what I did here is to create another script with a class with the parent class tk.Toplevel. This means the class becomes a subclass of it. We pass the parameter master wich is root, as you can see in the main.py to get a reference through the interface.
Also note that we imported the Mod1 script and later we reference the Model by Mod1.Model(). You could just import the Model with from Mod1 import Model instead of import Mod1. Also we imported from the main script the reference of tk to work with.
To pass a variabel like a string of your html, you can pass var through the interface. I made it like a kwarg(keyword argument), to make it necessary for the class to initialize you have to delete =None wich is a default vaulue.
Thats all we need to work it out. Have fun!
For more complex stuff you also could do a dictionary with keys and values.
main.py
import tkinter as tk
import Mod1
root = tk.Tk()
def callback():
data_dict = {'key a':'value a','key b':'value b'}
m1 = Mod1.Model(root, dct= data_dict)
b = tk.Button(text='click me', command=callback)
b.grid(column=0,row=1)
root.mainloop()
and Mod1.py
from __main__ import tk
class Model(tk.Toplevel):
def __init__(self, master, dct=None):
tk.Toplevel.__init__(self, master)
self.master = master
self.configure(bg="red", width=300, height=300)
b=tk.Button(self,text='print html', command=lambda:print(dct['key a']))
b.pack()
Related
I am writing an application in tkinter consisting of several modules in which there are classes. Each module to a separate page of the app. As I move the buttons between the pages "next", "previous" it opens a new window for me every time. How do I make it so that each time calling pages opens in the same window?
I give draft code.
thank you for your help :D
task1.py
import tkinter as tk
from Test.modul.task1 import FirstPage1
class FirstPage0:
def __init__(self, root):
self.root = root
def get_settings(self):
# Window settings
self.root.geometry("100x200")
def get_second_page(self):
FirstPage1(tk.Toplevel()).get_run_first_page()
def get_button(self):
# Add buttons
tk.Button(self.root, text="Start page", command=self.get_second_page).pack()
tk.Button(self.root, text="Exit", command=self.root.destroy).pack()
def get_run_first_page(self):
# Launching the application
self.get_settings()
self.get_button()
self.root.mainloop()
if __name__ == '__main__':
first = FirstPage0(tk.Tk())
first.get_run_first_page()
task2.py
import tkinter as tk
class FirstPage1:
def __init__(self, root):
self.root = root
def get_settings(self):
# Window settings
self.root.geometry("100x200")
def get_second_page1(self):
from Test.task import FirstPage0
FirstPage0(tk.Toplevel()).get_run_first_page()
def get_button(self):
# Add buttons
tk.Button(self.root, text="Back", command=self.get_second_page1).pack()
tk.Button(self.root, text="Exit", command=self.root.destroy).pack()
def get_run_first_page(self):
# Launching the application
self.get_settings()
self.get_button()
self.root.mainloop()
if __name__ == '__main__':
first = FirstPage1(tk.Tk())
first.get_run_first_page()
Solution
#AdrianSz, you wanted to make the buttons not stack under each other. There are three ways to do so. One, is to keep only one button and change its command and text parameters each time when the frames change. Another would be to unpack the button not needed and pack the button needed. The third would be to pack the buttons in the root window instead of frame and change the text and command parameters. I would recommend the second method as it is easier and less prone to errors.
Code
task1.py
import tkinter as tk
from Test.modul.task1 import FirstPage1
class FirstPage0:
def __init__(self, root):
self.root = root
def get_settings(self):
# Window settings
self.root.geometry("100x200")
def get_second_page(self):
self.root.pg_0_btn_start.pack_forget()
self.root_pg_0_btn_exit.pack_forget()
FirstPage1(tk.Toplevel()).get_run_first_page()
def get_button(self):
# Add buttons
self.root.pg_0_btn_start = tk.Button(self.root, text="Start page",
command=self.get_second_page).pack()
self.root_pg_0_btn_exit = tk.Button(self.root, text="Exit",
command=self.root.destroy).pack()
def get_run_first_page(self):
# Launching the application
self.get_settings()
self.get_button()
self.root.mainloop()
if __name__ == '__main__':
first = FirstPage0(tk.Tk())
first.get_run_first_page()
task2.py
import tkinter as tk
class FirstPage1:
def __init__(self, root):
self.root = root
def get_settings(self):
# Window settings
self.root.geometry("100x200")
def get_second_page1(self):
from Test.task import FirstPage0
self.root.pg_1_btn_back.pack_forget()
self.root_pg_1_btn_exit.pack_forget()
FirstPage0(tk.Toplevel()).get_run_first_page()
def get_button(self):
# Add buttons
self.root.pg_1_btn_back = tk.Button(self.root, text="Back", command=self.get_second_page1).pack()
self.root.pg_1_btn_exit = tk.Button(self.root, text="Exit", command=self.root.destroy).pack()
def get_run_first_page(self):
# Launching the application
self.get_settings()
self.get_button()
self.root.mainloop()
if __name__ == '__main__':
first = FirstPage1(tk.Tk())
first.get_run_first_page()
Note: I couldn't test this code from my side so if there are any errors, please comment on this answer
Suggestions
Since you are using modules for these, just make them inherit a class specified in a different file and operate them both from that file. There you can use self to access the methods of a subclass because the subclasses instantiate the base class and thus the self is a object of the subclass and is passed to the base class. The type of code you used is quite confusing too. I have added code to give you your wanted output using the principles I mentioned here. Hope this helped!
Here's the code of a window, using tkinter library and OOP. I want to make methods of class App private. But some of them, like method destroy in code below should be public
from tkinter import *
from tkinter import ttk
class App(Tk):
def __init__(self):
super().__init__()
# window settings
self.title("Private Attributes")
self.resizable(width=False, height=False)
root = App() # create window
root.title("Public Attributes") # this shouldn't work
ttk.Label(root, text="Close this window").pack()
ttk.Button(root, text="Close", command=root.destroy).pack() # this should work
root.mainloop()
If you want something that doesn't expose one or more Tk methods, you should use composition rather than inheritance. For example,
class App:
def __init__(self):
self._root = Tk()
self._root.title("Private Attributes")
self._root.resizable(width=False, height=True)
def mainloop(self):
return self._root.mainloop()
root = App()
root.title("Public Attributes") # AttributeError
root.mainloop() # OK
You'll need to decide if the ability to limit access to various Tk methods (remember, you can still access self._root directly, but the name suggests that you are responsible for any errors stemming from doing so) outweighs the amount of boilerplate you'll need to write to expose the methods you do want access to. (Reducing that boilerplate is beyond the scope of this answer.)
I am practicing to create a small project with different files to have a clean code. I want to show yellow frame from (fyellow.py) into (main.py) and input a label into it from (funbut.py) using Button's function. This is my code example: (3 Python files - main.py, fyellow.py, and funbut.py)
main.py
from tkinter import *
from fyellow import *
import funbut
root = Tk()
root.geometry("500x500")
# Show Yellow Frame into Main from (fyellow.py)
myframe = Frameyellow(root)
# Button with command - But_fun1
but1 = Button(root, text="Text",command=funbut.but_fun1)
but1.pack()
root.mainloop()
funbut.py
from tkinter import *
from fyellow import *
# Function of Button (but1) PROBLEM HERE! (ERROR - 'framey' is not defined)
def but_fun1():
label1 = Label(framey,text="LabelText")
label1.place(x=10,y=10)
fyellow.py
from tkinter import *
class Frameyellow:
def __init__(self,rootyellow):
self.rootyellow = rootyellow
self.framey = Frame(rootyellow, width=200,height=200,bg="yellow")
self.framey.pack()
Could explain what can I do to use the self.framey from file (fyellow.py) to avoid
error 'framey' is not defined?
So main.py file would look like this:
from tkinter import Tk, Button
from fyellow import FrameYellow
from funbut import place_label
root = Tk()
root.geometry("500x500")
my_frame = FrameYellow(root)
my_frame.pack()
but1 = Button(root, text="Text", command=lambda: place_label(my_frame))
but1.pack()
root.mainloop()
fyellow.py like this (tho kinda pointless to create a class whose sole purpose is to have the frame a different color, just use arguments and create a normal frame):
from tkinter import Frame
class FrameYellow(Frame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs, bg='yellow')
and funbut.py should be sth like this:
from tkinter import Label
def place_label(parent, text='Text', pos=(0, 0)):
Label(parent, text=text).place(x=pos[0], y=pos[1])
Also:
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.
So when I am calling the list directory code below directly then it's working fine. But when I am calling main.py which uses app class from gui which calls the list directory function, It prints "yoo" but prints an empty list instead of list of directories. I am stuck and can't figure out why that's happening. Any ideas?
Outputs:
When list directory called directly :
["/home/shubham/Desktop/movies/djangounchained.mkv"]
"yoo"
When called by main.py with same argument:
[]
"yoo"
Here is my main script
from gui import app
from list_directory import display_files
import tkinter as tk
root = tk.Tk()
directory = input("Enter directory name:")
root.geometry("400x300")
widgets_creator = app(root)
name = "get list"
directory_button = widgets_creator.create_button(name,function=display_files,path=directory)
root.mainloop()
Here is my gui script
import tkinter as tk
class app(tk.Frame):
def __init__(self,master):
super(app,self).__init__(master=master)
self.master = master
self.init_window()
def init_window(self):
# changing the title of our master widget
self.master.title("GUI")
# allowing the widget to take the full space of the root window
self.pack(fill=tk.BOTH, expand=1)
# creating a button instance
quitButton = tk.Button(self, text="Quit")
# placing the button on my window
quitButton.place(x=0, y=0)
def create_button(self,button_name,function,path):
button = tk.Button(self.master,text=button_name,command=lambda: function(path))
button.place(x=200,y=5)
return button
Here is my list_directory code:
import glob
def display_files(path):
x = glob.glob(path)
print(x)
print("yoo")
if __name__ == '__main__':
display_files("/home/shubham/Desktop/movies/*")
I might have found your problem. The code works fine, the problem is your argument. For example, if I enter '/Users/rudy/Desktop/*' when the input prompt comes up, I have the same result as you.
However, when I enter /Users/rudy/Desktop/* (without quotes), everything works fine. input() already saves the input as a string, so you don't need to add additional quotes.
I am trying to add a custom title to a window but I am having troubles with it. I know my code isn't right but when I run it, it creates 2 windows instead, one with just the title tk and another bigger window with "Simple Prog". How do I make it so that the tk window has the title "Simple Prog" instead of having a new additional window. I dont think I'm suppose to have the Tk() part because when i have that in my complete code, there's an error
from tkinter import Tk, Button, Frame, Entry, END
class ABC(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.parent = parent
self.pack()
ABC.make_widgets(self)
def make_widgets(self):
self.root = Tk()
self.root.title("Simple Prog")
If you don't create a root window, Tkinter will create one for you when you try to create any other widget. Thus, in your __init__, because you haven't yet created a root window when you initialize the frame, Tkinter will create one for you. Then, you call make_widgets which creates a second root window. That is why you are seeing two windows.
A well-written Tkinter program should always explicitly create a root window before creating any other widgets.
When you modify your code to explicitly create the root window, you'll end up with one window with the expected title.
Example:
from tkinter import Tk, Button, Frame, Entry, END
class ABC(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.parent = parent
self.pack()
self.make_widgets()
def make_widgets(self):
# don't assume that self.parent is a root window.
# instead, call `winfo_toplevel to get the root window
self.winfo_toplevel().title("Simple Prog")
# this adds something to the frame, otherwise the default
# size of the window will be very small
label = Entry(self)
label.pack(side="top", fill="x")
root = Tk()
abc = ABC(root)
root.mainloop()
Also note the use of self.make_widgets() rather than ABC.make_widgets(self). While both end up doing the same thing, the former is the proper way to call the function.
Here it is nice and simple.
root = tkinter.Tk()
root.title('My Title')
root is the window you create and root.title() sets the title of that window.
Try something like:
from tkinter import Tk, Button, Frame, Entry, END
class ABC(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
root = Tk()
app = ABC(master=root)
app.master.title("Simple Prog")
app.mainloop()
root.destroy()
Now you should have a frame with a title, then afterwards you can add windows for
different widgets if you like.
One point that must be stressed out is:
The .title() method must go before the .mainloop()
Example:
from tkinter import *
# Instantiating/Creating the object
main_menu = Tk()
# Set title
main_menu.title("Hello World")
# Infinite loop
main_menu.mainloop()
Otherwise, this error might occur:
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 2217, in wm_title
return self.tk.call('wm', 'title', self._w, string)
_tkinter.TclError: can't invoke "wm" command: application has been destroyed
And the title won't show up on the top frame.
Example of python GUI
Here is an example:
from tkinter import *;
screen = Tk();
screen.geometry("370x420"); //size of screen
Change the name of window
screen.title('Title Name')
Run it:
screen.mainloop();
I found this works:
window = Tk()
window.title('Window')
Maybe this helps?
Easy method:
root = Tk()
root.title('Hello World')
Having just done this myself you can do it this way:
from tkinter import Tk, Button, Frame, Entry, END
class ABC(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.parent = parent
self.pack()
ABC.make_widgets(self)
def make_widgets(self):
self.parent.title("Simple Prog")
You will see the title change, and you won't get two windows. I've left my parent as master as in the Tkinter reference stuff in the python library documentation.
For anybody who runs into the issue of having two windows open and runs across this question, here is how I stumbled upon a solution:
The reason the code in this question is producing two windows is because
Frame.__init__(self, parent)
is being run before
self.root = Tk()
The simple fix is to run Tk() before running Frame.__init__():
self.root = Tk()
Frame.__init__(self, parent)
Why that is the case, I'm not entirely sure.
self.parent is a reference to the actual window, so self.root.title should be self.parent.title, and self.root shouldn't exist.
widget.winfo_toplevel().title("My_Title")
changes the title of either Tk or Toplevel instance that the widget is a child of.
I found a solution that should help you:
from tkinter import Tk, Button, Frame, Entry, END
class ABC(Frame):
def __init__(self,master=None):
super().__init__(master)
self.pack()
self.master.title("Simple Prog")
self.make_widgets()
def make_widgets(self):
pass
root = Tk()
app = ABC(master=root)
app.mainloop()
Found at: docs.python.org