How to display my console logs on my tkinter window? - python

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?

Related

Why tkinter filedialog is looking different after using a pyglet window?

I have used a tkinter.filedialog with the pyglet library. But an old file explorer pops up when I open that filedialog. Normally it opens the native/modern file explorer of windows, but it is showing this behavior after importing the pyglet module. Any solution to get back the new filedialog?
Here is a basic code to reproduce the issue:
from tkinter import Tk, Button, filedialog
import pyglet #Try commenting this import to test the filedialog
root= Tk()
root.geometry("200x100")
window = pyglet.window.Window(20, 20, "Pyglet_window") #Pyglet window
def open_file():
file=filedialog.askopenfilename()
btn= Button(root, width=10,bg="#FFFFFF",fg="black", text="Open File",command=open_file)
btn.grid(padx=50,pady=20)
root.mainloop()
My results:

combining two tkinter files together to make a single program

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.

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()

Output result in a new Window

Basically, I have a root window that has a couple of buttons that runs different Python code, and it outputs the result into the console.
Is there a way to display the output into a new window (pop-up window) once the user clicks the button?
You could make a message box by doing
The Imports:
import Tkinter
from Tkinter import *
import tkMessageBox
import ttk
Heres the Code:
window = Tkinter.Tk()
window.wm_withdraw()
#centre screen message
window.geometry("1x1+"+str(window.winfo_screenwidth()/2)
+"+"+str(window.winfo_screenheight()/2))
tkMessageBox.showinfo(title=" ", message=" ")

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