I'm wondering if there is a way to draw tkinter windows over fullscreen applications, so far I have this:
from tkinter import *
#MAIN WINDOW
root = Tk()
root.title('Test Title')
root.geometry("500x200")
root.wm_attributes('-transparentcolor', root['bg'])
root.wm_attributes("-topmost", 1)
my_frame = Frame(root, width=500, height=200)
my_frame.pack(pady=20, ipady=20, ipadx=20)
#STAT TEXT
my_label = Label(my_frame, font=("Helvetica", 40), fg="#09d2f6")
my_label.config(text="TEST TEXT")
my_label.pack(pady=20)
root.mainloop()
This draws the window on top of all applications but not fullscreen ones. I had the idea to have a loop where it will constantly bring the window forward but have no idea how to do that.
This code will enable you to choose a picture to view on the fullscreen.
Your code will run in transparent mode above it - no problems
Press Escape key to exit
Try making your widget fullscreen using title button for weird effect!
Had to edit this due to the effect of filedialog on results.
Moved the attribute setting so that it is invoked after image is loaded.
import os
import tkinter as tk
from tkinter import filedialog
def closer( ev ):
ev.widget.destroy()
# FULL SCREEN
master = tk.Tk()
master.rowconfigure( 0, weight = 1 )
master.columnconfigure( 0, weight = 1 )
master.bind( "<Escape>", closer )
pathfile = filedialog.askopenfilename( title = 'pick mage' )
my_image = tk.PhotoImage( file = pathfile ).zoom( 2,2 )
label = tk.Label( master, text = 'Image', compound = "top", image = my_image )
label.grid(row=0, column=0,sticky='nsew')
master.wm_attributes("-fullscreen", 1)
# removed for first time use - unrem this for second time
# master.wm_attributes("-topmost", 1)
# Your code
root = tk.Toplevel(master)
root.title('Test Title')
root.geometry("500x200")
root.bind( "<Escape>", closer )
root.wm_attributes('-transparentcolor', root['bg'])
root.wm_attributes("-topmost", 1)
my_frame = tk.Frame(root, width=500, height=200)
my_frame.pack(pady=20, ipady=20, ipadx=20)
#STAT TEXT
my_label = tk.Label(my_frame, font=("Helvetica", 40), fg="#09d2f6")
my_label.config(text="TEST TEXT")
my_label.pack(pady=20)
master.mainloop()
Related
I'm creating a program by learning from youtube tutorials (I'm a complete beginner) and I have come to some difficulties. This time, I'm trying to create a scrollbar, and I want my widgets to stay on the center of my window, not the left (I'm following the Codemy.com tutorial on scrollbars).
Here is the current aspect of my program:
with scrollbar
And here is how I want it to look:
without scrollbar
This is my code right now:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
my_canvas = tk.Canvas(root)
my_canvas.pack(side = "left", fill = "both", expand = 1)
my_scrollbar = tk.Scrollbar(root, orient = "vertical", command = my_canvas.yview)
my_scrollbar.pack(side = "right", fill = "y")
my_canvas.configure(yscrollcommand = my_scrollbar.set)
my_canvas.bind("<Configure>", lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))
my_frame = tk.Frame(my_canvas)
for i in range(100):
my_label = tk.Label(my_frame, text = "Label")
my_label.pack()
my_canvas.create_window((0,0), window = my_frame, anchor = "nw")
root.mainloop()
Include width = 600, anchor = "nw" in my_canvas declaration.
my_canvas.create_window((0,0), window = my_frame, width = 600, anchor = "nw")
I am trying to add an image to a button, but I have got some issues when I try to execute the current code. All it shows is an image with no words. I can't even see the button either. Is there some way of fixing my current code?
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
root.mainloop()
You need to pack (or grid) your button in the window, here is how you could do:
import tkinter as tk
from tkinter import PhotoImage
def print_hello():
print('hello')
root = tk.Tk()
root.geometry("960x600")
imagetest = PhotoImage(file="giftest.gif")
button_qwer = tk.Button(root, text="asdfasdf", image=imagetest, command=print_hello)
button_qwer.pack() # <-- don't forget to place the button in the window
root.mainloop()
You can have both text and image displayed on your button, using the compound option, like this:
button_qwer = tk.Button(root, image=imagetest, text="asdfasdf", compound="top", command=print_hello)
compound options are bottom, center, left, none, right, or top
You are making the button successfully but you are not drawing it onto the screen/interface. Use pack , place or grid.
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
Your full code can be like:
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
root.mainloop()
Objective:
I am trying to create a GUI with a portion of the screen having "tabs" (information displayed can be changed based on selected tab), and another portion constantly displaying the same thing.
import ttk
import Tkinter
def demo():
#root = tk.Tk()
schedGraphics = Tkinter
root = schedGraphics.Tk()
root.title("Testing Bot")
universal_height = 606
canvas = schedGraphics.Canvas(root,width = 900, height = universal_height)
nb = ttk.Notebook(root)
# adding Frames as pages for the ttk.Notebook
# first page, which would get widgets gridded into it
page1 = ttk.Frame(nb,width = 300,height = universal_height)
# second page
page2 = ttk.Frame(nb,width = 300,height = universal_height)
nb.add(page1, text='One')
nb.add(page2, text='Two')
#
nb.grid()
day_label = schedGraphics.Label(page1, text="Day1:")
day_label.pack()
day_label.place(x=0, y=30)
day_label = schedGraphics.Label(page2, text="Day2:")
day_label.pack()
day_label.place(x=0, y=30)
canvas.create_rectangle(50,500,300,600,fill = "red")
canvas.grid()
root.mainloop()
if __name__ == "__main__":
demo()
Problems:
In the current configuration the tabs are located in the MIDDLE of the screen not on the left side.
If I change canvas.grid() to canvas.pack() it doesn't actually open any window?
The rectangle on canvas does not appear!
Thank you.
To do this, when gridding your notebook, pass the argument column and choose 0, so that it will be located at the far left, like this:
nb.grid(column=0)
That's because you have to chose, for your tkinter app, between .grid() and .pack(): the two are not compatible. As you used .grid() before, the window won't open and a TclError pops up.
Your canvas is in fact hidden under the notebook. To fix that, set the row argument when using grid to 0, so that it is at the top, like this:
canvas.grid(column=1, row=0)
Final code:
import Tkinter
import ttk
def demo():
#root = tk.Tk()
schedGraphics = Tkinter
root = schedGraphics.Tk()
root.title("Testing Bot")
universal_height = 606
nb = ttk.Notebook(root)
# adding Frames as pages for the ttk.Notebook
# first page, which would get widgets gridded into it
page1 = ttk.Frame(nb, width= 300,height = universal_height)
# second page
page2 = ttk.Frame(nb,width = 300,height = universal_height)
nb.add(page1, text='One')
nb.add(page2, text='Two')
nb.grid(column=0)
day_label = schedGraphics.Label(page1, text="Day1:")
day_label.pack()
day_label.place(x=0, y=30)
day_label = schedGraphics.Label(page2, text="Day2:")
day_label.pack()
day_label.place(x=0, y=30)
canvas = schedGraphics.Canvas(root, width=900, height=universal_height)
canvas.create_rectangle(50, 500, 300, 600, fill="red")
canvas.grid(column=1, row=0)
root.mainloop()
if __name__ == "__main__":
demo()
I hope this helps !
In the following code, I want to display picture1.jpg as soon as button1 is pressed. After some time, I want to display picture2.jpg. In the following case, only picture2.jpg is displayed 5 seconds after the button1 is pressed. How can I change pictures?
from Tkinter import *
from PIL import Image, ImageTk
import time
def show_image():
while True:
image = Image.open("picture1.jpg")
tk_img = ImageTk.PhotoImage(image)
x = canvas.create_image(400, 300, image=tk_img)
canvas.itemconfigure(x, state=NORMAL)
# I want to show picture1.jpg now
time.sleep(5) #This is a dummy because it takes a while for picture2.jpg
#Now I can show picture2.jpg
image = Image.open("picture2.jpg")
tk_img = ImageTk.PhotoImage(image)
x = canvas.create_image(400, 300, image=tk_img)
canvas.itemconfigure(x, state=NORMAL)
yield
root = Tk()
canvas = Canvas(root, width=850, height=750)
canvas.grid(row=0, column=0)
button1 = Button(
root, text="Insert next device and then take a picture", command=show_image().next, anchor='w',
width=50, activebackground="#33B5E5" ,fg = "blue" , font=("Arial", 24))
button1.grid(row=1, column=0)
root.mainloop()
First, as a general rule you should never call sleep in the main thread of a GUI. It causes your whole program to pause -- buttons won't work, the screen won't update, etc.
Tkinter has a way to run code in the future. Change the image, and then use this function to change it back later. Roughly speaking, it would look like this:
def show_image(path):
image = Image.open(path)
tk_img = ImageTk.PhotoImage(image)
x = canvas.create_image(400, 300, image=tk_img)
def switch_images():
show_image("picture1.jpg")
root.after(5000, show_image, "picture2.jpg")
This seems like a pretty straightforward question, but i am having trouble displaying a jpg image when a button is clicked. Here is my code (without the button code for the sake of time):
from tkinter import *
#screen stuff here
canvas = Canvas(app)
canvas.grid(row = 0,column = 0)
photo = PhotoImage(file = "test.jpg")
canvas.create_image(0,0, image = photo)
def show_image():
global canvas
global photo
canvas.create_image(0,0, image = photo)
#button that calls the function down here
Thanks!
This works with Python2:
import Tkinter as tk
import ImageTk
def show_image():
x = canvas.create_image(125, 125, image=tk_img)
while True:
print('show')
canvas.itemconfigure(x, state=tk.NORMAL)
button.configure(text = 'Hide')
yield
print('hide')
canvas.itemconfigure(x, state=tk.HIDDEN)
button.configure(text = 'Show')
yield
root = tk.Tk()
canvas = tk.Canvas(root, width=250, height=250)
canvas.grid(row=0, column=0)
tk_img = ImageTk.PhotoImage(file='image.png')
button = tk.Button(
root, text="Show", command=show_image().next, anchor='w',
width=10, activebackground="#33B5E5")
button.grid(row=1, column=0)
root.mainloop()
In Python3, PhotoImage can open GIF, PPM/PGM images. To open other formats, you may need to install Pillow (a fork of the PIL project for Python3).