Hello how can I make a full screen tkinter.Canvas? Can you help me? This is my code:
import tkinter
import datetime
import sys
import os
uvodcanvas = tkinter.Canvas(width=400,height=200,bg="white")
uvodcanvas.pack()
tkinter.mainloop()
You need to make your main window full-screen, then configure the canvas to occupy the whole main window:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True) # make main window full-screen
canvas = tk.Canvas(root, bg='white', highlightthickness=0)
canvas.pack(fill=tk.BOTH, expand=True) # configure canvas to occupy the whole main window
root.mainloop()
You can set root.attributes to fullscreen.
The following example shows how to toggle from fullscreen to a sized window, and vice-versa, upon pressing the Escape key
import tkinter as tk
def toggle_fs(dummy=None):
state = False if root.attributes('-fullscreen') else True
root.attributes('-fullscreen', state)
if not state:
root.geometry('300x300+100+100')
root = tk.Tk()
tk.Canvas(root, bg='cyan').pack(expand=True, fill=tk.BOTH)
root.attributes('-fullscreen', True)
root.bind('<Escape>', toggle_fs)
root.mainloop()
Related
I'm a Python beginner that trying to learn my way through Tkinter, and I need your help.
Let say I create a simple button like this:
import tkinter as tk
window = tk.Tk()
button = tk.Button(text="Hello World!")
button.pack()
window.mainloop()
Is there a way that I can hide and then display the text again? Given that I can create two more buttons that will do the job of hiding and displaying. I have tried to use button.pack_forget(), but it will hide the entire button instead.
Any help would be appreciated.
To make it look like the buttons text vanishes,you can make the text color as same as the background color via cget method.
import tkinter as tk
def hide_text():
color = button['bg']
button.config(foreground=color, activeforeground=color)
window = tk.Tk()
button = tk.Button(text="Hello World!",command=hide_text)
button.pack()
window.mainloop()
Approach 2 ttk.Button:
import tkinter as tk
from tkinter import ttk
def hide_text():
button.config(text='')
window = tk.Tk()
button = ttk.Button(text="Hello World!",width=100,command=hide_text)
button.pack()
window.mainloop()
Approach3 using style:
import tkinter as tk
from tkinter import ttk
def change_button_style(event):
widget = event.widget
if widget['style'] == 'TButton':
widget.configure(style='VanishedText.TButton')
else:
event.widget.config(style='TButton')
BACKGROUND = '#f0f0f0'
FOREGROUND = '#000000'
window = tk.Tk()
window.configure(bg=BACKGROUND)
style = ttk.Style()
style.theme_use('default')
button = ttk.Button(text="Hello World!",style='VanishedText.TButton')
button.bind('<ButtonPress-1>',change_button_style)#,command=change_button_style
button.pack()
style.map('VanishedText.TButton',
foreground =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
background =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
focuscolor=[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)])
window.mainloop()
You Can Simply Config And Set Text :
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
def hidetext():
button.config(text="")
button=ttk.Button(win,text="FooBar",command=hidetext)
button.pack()
win.mainloop()
Why can't I use zoomed state in non resizable window?
import tkinter as tk
root = tk.Tk()
root.resizable(False, False)
root.state('zoomed')
root.mainloop()
I'm trying to create a python program tkinter that, upon the pressing of a button, opens a new full screen tkinter window containing an image and plays an audio file - here's my code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.png"))
label = Label(window, image=img).pack()
playsound("song.mp3")
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
(my image and audio file are both on the desktop with my python file)
However, when I run my code, when I press the button, the audio plays but no second tkinter window opens.
I've tried to destroy() the buttonWindow and have tried many different ways of including an image on a tkinter window - if I remove the line of code using PhotoImage(), the window appears (obviously I then get a syntax error stating that 'img' is not defined).
How could I solve this?
Thanks,
Louis
I had similar problem.
But i resolve this trial and error method.
First of all i don't use pillow library and put image into Label
You should try define Photoimage object NOT IN FUNCTION
Example:
It will work:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
def popUp():
popff = tk.Toplevel(root)
popff.geometry("400x200")
labelImgff = tk.Label(popff, image=imgff, bg="white")
labelImgff.grid(row=0, column=0)
imgff = tk.PhotoImage(file="/home/user/image.png")
btnff = tk.Button(text="startPOP", command=popUp)
btnff.grid(column=0,row=0)
root.mainloop()
effect of action
Your playsound() command is blocking execution. The playsound() command has an optional field 'block', which is True by default. Changing this to False will continue execution and allow mainloop() to continue.
Second, just call label.draw() to draw your image to the TopLevel window.
Here's the code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.jpeg"))
label = Label(window, image=img).pack()
playsound("song.mp3",block=False)
label.draw()
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
buttonWindow.mainloop()
Cheers!
I have a simple code that opens an exe. This exe makes a pop-up image using Tkinter but there is an option to delete or minimise the photo. How do I get rid of this option?
here is my code
and here is the TK option I was talking about
[![enter image description here]
and here is the TK option I was talking about
You can try:
CODE_1: where it will show minize, maximize and exit but it will not work
from tkinter import *
window = Tk()
window.title("Grand Canyon")
canvas = Canvas(window, width=500, height=500)
canvas.pack()
#window.overrideredirect(1)
window.attributes('-disabled', True)
window.resizable(0,0)
window.mainloop()
CODE_2: where it will NOT show minize, maximize and exit because it will pack into the top of your window
from tkinter import *
window = Tk()
window.title("Grand Canyon")
canvas = Canvas(window, width=500, height=500)
canvas.pack()
window.overrideredirect(1)
#window.attributes('-disabled', True)
window.resizable(0,0)
window.mainloop()
So I'm trying to make a code that prints the events/actions that happens on the tkinter area. When I run the script I don't get an error but when I'm clicking on the graphics area nothing prints out.
import tkinter
canvas = tkinter.Canvas(width=640, height=480)
canvas.pack()
def function1(event):
print(repr(event))
canvas.bind("ButtonPress-1", function1)
canvas.mainloop()
You need to define the instance of tkinter.Tk() and use it as root. The following implementation works for me as expected:
import tkinter
root = tkinter.Tk()
def function1(event):
print(repr(event))
canvas = tkinter.Canvas(root, width=640, height=480)
canvas.bind("<ButtonPress-1>", function1)
canvas.pack()
root.mainloop()