Reputation: 487
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()
Upvotes: 0
Views: 229
Reputation: 8057
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()
Upvotes: 1