Ok, now it's time to pay attention. We will spawn a new window in our Tkinter app by clicking on button.
To do that we will need dedicated function. Just as with a primary window, for spawned window we need to set geometry, which in most cases will be smaller than for a primary app. In this case, 300 x 300.
In that new function we will have independent button that will call some normal function, like to ping Google, or something simple like that.
As with any button, we need to pack it. At the end of net_window() function we will have mainloop:
def net_window():
net_win = Toplevel()
net_win.geometry("300x300")
but_google = Button(net_win, text = "Ping Google", command = ping_google)
but_google.pack(side = LEFT, anchor = NW)
net_win.mainloop()
Our normal function (that is doing some useful operation) will just ping Google:
def ping_google():
os.system("ping google.com")
Now we need to have primary button but_prim that will spawn new app window:
but_prim = Button(top_win, text = "Net Window", command = net_window)
but_prim.pack(side = LEFT, anchor = NW)
You are advised to check corresponding YT tutorial on How to Spawn New Window in Tkinter.
This is full working script:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("500x500")
def net_window():
net_win = Toplevel()
net_win.geometry("300x300")
but_google = Button(net_win, text = "Ping Google", command = ping_google)
but_google.pack(side = LEFT, anchor = NW)
net_win.mainloop()
def ping_google():
os.system("ping google.com")
but_prim = Button(top_win, text = "Net Window", command = net_window)
but_prim.pack(side = LEFT, anchor = NW)
top_win.mainloop()
If you don't understand a previous tutorial, give yourself some time. There is no purpose in code memorization, just try to understand an approach. Perhaps code before looks a little bit complicated, bit in fact it is not.
A little bit bigger example
In this example we are improving previous code in such ways that we can have one more button that will spawn new window, and in that window we will have some normal function which will run Notepad. Function to spawn new Notepad window will be this one:
def notepad_window():
note_window = Toplevel()
note_window.geometry("300x300")
but_note = Button(note_window, text = "Notepad", command = run_notepad)
but_note.pack(side = LEFT, anchor = NW)
note_window.mainloop()
Function that will run Notepad is this, simple one:
def run_notepad():
os.system("notepad")
Now, button that will spawn Window where is button to run Notepad is this one:
but_prim_notepad = Button(top_win, text = "Run Notepad", command = notepad_window)
but_prim_notepad.pack(side = LEFT, anchor = NW)
Again as before you are advised to check corresponding YT tutorial on How to Spawn New Window in Tkinter.
This is full working script:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("500x500")
def net_window():
net_win = Toplevel()
net_win.geometry("300x300")
but_google = Button(net_win, text = "Ping Google", command = ping_google)
but_google.pack(side = LEFT, anchor = NW)
net_win.mainloop()
def notepad_window():
note_window = Toplevel()
note_window.geometry("300x300")
but_note = Button(note_window, text = "Notepad", command = run_notepad)
but_note.pack(side = LEFT, anchor = NW)
note_window.mainloop()
def ping_google():
os.system("ping google.com")
def run_notepad():
os.system("notepad")
but_prim = Button(top_win, text = "Net Window", command = net_window)
but_prim.pack(side = LEFT, anchor = NW)
but_prim_notepad = Button(top_win, text = "Run Notepad", command = notepad_window)
but_prim_notepad.pack(side = LEFT, anchor = NW)
top_win.mainloop()
No comments:
Post a Comment