combining two tkinter files together to make a single program - python

I have two tkinter files, one with a welcome screen and the other with the selection menu, I have created two separate tkinter files and combine them to make it a single program where the welcome screen dissolves after 10 seconds and the selection menu appears.
my code:
welcome screen:
from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *
#icon
root = Tk()
root.title("-----")
root.geometry("425x508")
root.iconbitmap('left this on purpose')
#welcome screen
welcome = ImageTk.PhotoImage(Image.open("left this on purpose"))
lbl = Label(image=welcome)
lbl.pack()
root.after(10000, lambda: root.destroy())
selection menu screen:
from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *
#icon
root = Tk()
root.title("selection")
root.geometry("325x396")
root.iconbitmap("left this on purpose")
select = ImageTk.PhotoImage(Image.open("left this on purpose"))
lbl2 = Label(image=select)
lbl2.pack()
root.mainloop()

You didn't add a way to display the welcome screen, however, if that's what you wanted:
main.py -
from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *
from selectionmenu import app
#icon
root = Tk()
root.title("-----")
root.geometry("425x508")
# stuff you want to do
root.after(10000, app)
selectionmenu.py -
from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *
# icon
def app():
root = Tk()
root.title("selection")
root.geometry("325x396")
root.mainloop()
I don't think this is the way to proceed by having two instances of Tk(), but using the Toplevel widget doesn't provide a way to delete the root window and using the toplevel.

Related

How to call another python script from a .py file in a new window

I ran into a problem where I want to click a button on a fullscreen app.
test1
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
root = Tk()
root.title('Gamesim')
root.geometry('500x400')
def cmdopen():
os.system('C:\Users\User\Desktop\test2.py')
btn = Button(text='test', command=cmdopen)
btn.pack()
root.mainloop()
test2
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
root = Tk()
root.title('Gamesim')
root.geometry('1870x1080')
root.attributes("-topmost", True)
btn = Button(text='test2')
btn.pack()
root.mainloop()
What it does it displays the test2 interface, but test one stops responding. What I want is that the test2 will apear above and both will respond and are diffrent windows.
Im bad in english so sorry if I have some problems.
If you're okay with having one "master" window that keeps track of the other windows, then you can do something like this:
from tkinter import *
from tkinter.ttk import *
from functools import partial
class subWindow(Toplevel):
def __init__(self, master=None):
super().__init__(master=master)
def createSubwindow(master):
"""Creates a subWindow of 'master' and sets it's options"""
subWin = subWindow(master)
subWin.title('SubWindow')
subWin.geometry('500x400')
subWin.attributes("-topmost", True)
btn = Button(subWin, text='Button Inside of SubWindow')
btn.pack()
# Creating the master-window
root = Tk()
root.title('MasterWindow')
root.geometry('500x400')
# Creates a partial of the createSubwindow, so that we can execute it easier in the button.
subWinPartial = partial(createSubwindow, root)
# Installs the button, with the partial function as a command.
btn = Button(root, text='Create Sub Window', command=subWinPartial)
btn.pack()
# Runs the mainloop, that handles all the windows.
root.mainloop()

How to overwrite an image in Tkinter

I created a simple image opening program which opens the image selected from filedialog by clicking a button, but wherever I select another image it just appears under the current image
I want the next image selected to be replaced by the old image.
Plz help what should I do
from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
root=Tk()
root.title('Image')
def open():
global my_img
root.filename = filedialog.askopenfilename(initialdir='/GUI',title='Select A File',filetypes=(('jpg files','*.jpg'),('png files','*.png'),('all files','*.*')))
my_img = ImageTk.PhotoImage(Image.open(root.filename))
my_image_lbl = Label(image=my_img).pack()
my_btn = Button(root,text='Open File Manager',command=open).pack()
root.mainloop()
You should create the my_image_lbl outside open() and update its image inside the function:
from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
root=Tk()
root.title('Image')
def open():
filename = filedialog.askopenfilename(initialdir='/GUI',title='Select A File',filetypes=(('jpg files','*.jpg'),('png files','*.png'),('all files','*.*')))
if filename:
my_image_lbl.image = ImageTk.PhotoImage(file=filename)
my_image_lbl.config(image=my_image_lbl.image)
Button(root,text='Open File Manager',command=open).pack()
my_image_lbl = Label(root)
my_image_lbl.pack()
root.mainloop()

How to display my console logs on my tkinter window?

from tkinter import Tk
from tqdm import tqdm
root = Tk()
root.geometry('300x300')
root.mainloop()
for i in tqdm(range(1000)):
pass
Can it see the process bar inside m tkinter window?

How I used tkinter button to Import files

I would like to run a python program and when clicking a button in a (tkinter) window become import files form any direction and store it to another direction and in database .
import tkinter as tk
from tkinter import filedialog
def UploadAction(event=None):
filename = filedialog.askopenfilenames()
print('Selected:', filename)
root = tk.Tk()
button = tk.Button(root, text='import files', command=UploadAction)
button.grid(row=1,column=1,padx=10,pady=10)
root.mainloop()

How to set a tkinter windows on top when running a slide app (e.g. Keynote)

I want to show some dynamic text in a window on top when playing a slide show in fullscreen mode. I use the OS X.
I had tried the method attributes("-topmost", 1). Not work...
Thanks
Here is my code:
# coding=utf-8
from tkinter import *
from tkinter import ttk
root = Tk()
root.title(u"弾幕")
root.attributes("-alpha", 0.3)
root.attributes("-topmost", 1)
root.mainloop()

Categories

Resources