Python 3 Tkinter Borderless fullscreen application - python-3.x

I have been having a problem whilst creating a Python 3 tkinter application. I am currently developing using a Mac OSX system, but I normally use a Windows OS system.
I would like the application to occupy the entire screen without the Window Manager's titlebar and frame being around the application, often referred to a Fullscreen Borderless Window in gaming.
I have tried using root.attributes("-fullscreen", True) with root.overrideredirect(True) and root.wm_attributes("-topmost", 1). However the inclusion of the root.overrideredirect(True) line doesn't allow it to go proper fullscreen; it still shows the Mac Dock and Taskbar, and it also breaks my keystroke bindings in the application. Without the root.overrideredirect(True) line, the application does go into full screen mode (hiding the dock and the task bar), but the window does not fill the entire screen; it leaves a gap at the bottom, and it also retains the window manager's title bar and frame/border.
Here is an example of my code:
import tkinter as tk
class App(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Fullscreen Application")
self.pack(fill="both", expand=True, side="top")
self.parent.wm_state("zoomed")
self.parent.bind("<F11>", self.fullscreen_toggle)
self.parent.bind("<Escape>", self.fullscreen_cancel)
self.fullscreen_toggle()
self.label = tk.Label(self, text="Fullscreen", font=("default",120), fg="black")
self.label.pack(side="top", fill="both", expand=True)
def fullscreen_toggle(self, event="none"):
self.parent.focus_set()
self.parent.overrideredirect(True)
self.parent.attributes("-fullscreen", True)
self.parent.wm_attributes("-topmost", 1)
def fullscreen_cancel(self, event="none"):
self.parent.overrideredirect(False)
self.parent.attributes("-fullscreen", False)
self.parent.wm_attributes("-topmost", 0)
self.centerWindow()
def centerWindow(self):
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
w = sw*0.7
h = sh*0.7
x = (sw-w)/2
y = (sh-h)/2
self.parent.geometry("%dx%d+%d+%d" % (w, h, x, y))
if __name__ == "__main__":
root = tk.Tk()
App(root).pack(side="top", fill="both", expand=True)
root.mainloop()
I hope that someone is able to help! Thank you!
EDIT: I have just tested this on my Windows computer. Without the self.parent.overrideredirect(True), it creates the app and works perfectly as desired (fullscreen without window manager border or title bar). This must just be an OSX problem.

To fix your OS-X Problem i will provide a solution that fixes a similar problem for me. (Had some Issues using fullscreen in between Linux and Windows)
You wanted to get rid of the Window Managers Bar? Take a look at the docs it states an option that removes the window managers items by using -toolwindow option.
Concerning the size of your Application here is what helped me using linux - a "manual zoom":
class MyClass(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.overrideredirect(True) # depending on your needs
self.attributes("-toolwindow", 1) # this line removes the window managers bar
try: # Automatic zoom if possible
self.wm_state("zoomed")
print("Using automatic zoom")
except tk.TclError: # Manual zoom
# Bad Argument Error is a TclError
print("Using manual zoom")
# get the screen dimensions
width = self.winfo_screenwidth()
height = self.winfo_screenheight()
# build a geometry string.
# form: width x height + x_offset + y_offset
geom_string = "%dx%d+0+0" % (width, height)
self.wm_geometry(geom_string)
Please note that I am not using an unconfigured tk.Tk() - Instance here - my class is the tk.Tk() - Instance. So I do not need to overwrite the parent but only "myself" speaking of the POV of the class.

#!/usr/bin/python
import Tkinter as tk
class App(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Fullscreen Application")
self.pack(fill="both", expand=True, side="top")
self.parent.wm_state("zoomed")
self.parent.bind("<F11>", self.fullscreen_toggle)
self.parent.bind("<Escape>", self.fullscreen_cancel)
self.fullscreen_toggle()
self.label = tk.Label(self, text="Fullscreen", font=("default",120), fg="black")
self.label.pack(side="top", fill="both", expand=True)
def fullscreen_toggle(self, event="none"):
self.parent.focus_set()
self.parent.overrideredirect(True)
self.parent.overrideredirect(False) #added for a toggle effect, not fully sure why it's like this on Mac OS
self.parent.attributes("-fullscreen", True)
self.parent.wm_attributes("-topmost", 1)
def fullscreen_cancel(self, event="none"):
self.parent.overrideredirect(False)
self.parent.attributes("-fullscreen", False)
self.parent.wm_attributes("-topmost", 0)
self.centerWindow()
def centerWindow(self):
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
w = sw*0.7
h = sh*0.7
x = (sw-w)/2
y = (sh-h)/2
self.parent.geometry("%dx%d+%d+%d" % (w, h, x, y))
if __name__ == "__main__":
root = tk.Tk()
App(root).pack(side="top", fill="both", expand=True)
root.mainloop()

Related

Is there a way to make tkinter windows work independantly Tkinter?

I have been looking to create a code that opens a second tkinter window to display stuffs live while a program is running on my main window. However, doing so, my main window gets frozen during 5s and then displays stuff on my second window when it is completed.
Is there a way to live display in the second window ?
My code below:
import tkinter as tk
from tkinter import ttk
import time
class PopUpLog(tk.Tk):
def __init__(self, parent):
tk.Tk.__init__(self)
self.y=5
tk.Button(self.master, text="Write in pop-up", command=self.write).pack(side="left")
# canvas
frameL = tk.Frame(self)
frameL.pack(side="left", fill="both")
self.canvasL = tk.Canvas(frameL, height=800, width=800)
self.canvasL.pack(fill="both", expand=True)
# scrollbar
vsb = ttk.Scrollbar(self, orient="v", command=self.canvasL.yview)
vsb.pack(side="left", fill="y")
self.canvasL.configure(yscrollcommand=vsb.set)
self.canvasL.bind("<Configure>", lambda e:self.canvasL.configure(scrollregion=self.canvasL.bbox("all")))
def write(self, text="hi im a pop-up"):
for i in range(5):
self.canvasL.create_text(5, self.y, anchor='nw', justify='left', text=text)
self.y += 25
time.sleep(1)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Button(self, text="Open window", command=self.popup).pack(side="left")
def popup(self):
self.top = PopUpLog(self)
self.top.geometry("400x400")
self.top.title("pop-up")
self.top.mainloop()
if __name__ == "__main__":
root = App()
root.mainloop()
So far, the program runs for 5s and then displays everything in self.top. BUT I need a live display (made every time create_text is called) in self.top but I can't even get that.
I am sorry if this is redundant to another question asked but I couldn't find helpful enough information.
Thanks a lot !
time.sleep is the reason why your window is freezing. This is the case for virtually any GUI toolkit. If you want the updates to happen incrementally you can use the after method which will execute the callback you assign after a certain number of milliseconds.
Also there should only be one mainloop. There is no need to start one per window and doing so could cause problems.
Here is an example using the after method:
class PopUpLog(tk.Tk):
def __init__(self, parent):
tk.Tk.__init__(self)
self.y=5
self.c=5 # counter
tk.Button(self.master, text="Write in pop-up", command=self.write).pack(side="left")
# canvas
frameL = tk.Frame(self)
frameL.pack(side="left", fill="both")
self.canvasL = tk.Canvas(frameL, height=800, width=800)
self.canvasL.pack(fill="both", expand=True)
# scrollbar
vsb = ttk.Scrollbar(self, orient="v", command=self.canvasL.yview)
vsb.pack(side="left", fill="y")
self.canvasL.configure(yscrollcommand=vsb.set)
self.canvasL.bind("<Configure>", lambda e:self.canvasL.configure(scrollregion=self.canvasL.bbox("all")))
def write(self, text="hi im a pop-up"):
if self.c > 0:
self.canvasL.create_text(5, self.y, anchor='nw', justify='left', text=text)
self.y += 25
self.c -= 1 # reduce counter
self.after(1000, self.write) # call again in 1 second
else:
self.c = 5 # when counter is 0 reset counter
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Button(self, text="Open window", command=self.popup).pack(side="left")
def popup(self):
self.top = PopUpLog(self)
self.top.geometry("400x400")
self.top.title("pop-up")
if __name__ == "__main__":
root = App()
root.mainloop()

Python3 Tkinter - problem with expanding one frame to fit root window

Hi there (this is my first question)
I am building an app with Tkinter as the GUI. I want multiple frames to expand to fill out the entire root window.
With the code below, I expected the bottom (green) frame to expand all the way up to the top (cyan) frame. Instead, it stays at the bottom, and there is a "frame-less" white area between the two frames.
screenshot of an actual result when code is run
This is the code, I am executing (methods that do not mess with frame layout has been shortened out):
class CreateWindow:
def __init__(self, master, screen):
self.master = master
self.master.geometry('300x400')
self.master.title("THE PROGRAM")
self.screen = screen
self.menu_bar = Menu(self.master)
self.setup_menu = Menu(self.menu_bar)
self.setup_bar()
self.main_menu = Menu(self.menu_bar)
self.main_bar()
self.diary_menu = Menu(self.menu_bar)
self.diary_bar()
self.master.config(menu=self.menu_bar)
# self.master.grid_columnconfigure(0, weight=1) # What is difference between these two and the two below?
# self.master.grid_rowconfigure(0, weight=1)
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
self.top_menu(self.master) # TODO: Make this menu actively do stuff
if self.screen == "setup":
setup = SetupScreen(self.master)
elif self.screen == "main":
setup = MainScreen(self.master)
elif self.screen == "diary":
setup = DiaryScreen(self.master)
else:
raise TypeError("wrong screen")
def setup_bar(self): ...
def main_bar(self): ...
def diary_bar(self): ...
def top_menu(self, window): # Defines top frame : placeholder for future menu
top = tk.Frame(window, bg='cyan', pady=5)
top.grid(row=0, sticky='new')
button = tk.Button(top, text="Setup", command=self.do_nothing)
button.grid(row=0, column=0)
button = tk.Button(top, text="Main", command=self.do_nothing)
button.grid(row=0, column=1)
button = tk.Button(top, text="Diary", command=self.do_nothing)
button.grid(row=0, column=2)
top.columnconfigure(0, weight=1)
top.columnconfigure(1, weight=1)
top.columnconfigure(2, weight=1)
def do_nothing(self): ...
def b_exit(self): ...
"""This class contains methods, that create and manage the setup screen.
I want the green frame to expand all the way up to the cyan (top menu) """
class SetupScreen(CreateWindow):
def __init__(self, master):
self.master = master
self.menu = tk.Frame(self.master, bg='green')
self.menu.grid(row=1, sticky='new')
self.menu.columnconfigure(0, weight=1) # Again, what is difference between 'grid_'or not?
self.menu.grid_rowconfigure(1, weight=1) #I have tried setting index to both 0 and 1, no difference
self.create_buttons()
def create_buttons(self): ...
def personal_details(self): ...
def start_new(self):
pass
if __name__ == "__main__":
files = FileHandler() #Class meant to be handling file operations - currently only sets a boolean to false, that makes the app start with setup screen
ap = files.active_program
print(ap)
root = tk.Tk()
if not files.active_program: #based on the boolean from FileHandler class, this starts the setup screen
top_menu = CreateWindow(root, "setup")
else:
top_menu = CreateWindow(root, "main")
root.mainloop()
It looks like you're trying to create a notebook widget with several tabs.
So I would suggest you use ttk.Notebook instead of re-inventing it yourself.

Tkinter error: bad window path name when deleting frames dynamically

Im trying to recreate a little version of trello in tkinter. Right now im stuck I have a problem when I want to delete frames in a different order. For example: I click on the button and a new frame is generated if I delete that everything works. If I create 3 frames I have to remove them in the same order as I have created them. So I think my problems lies in the pop function but I dont know how to access them manually. When i change the pop function to (1) then I have to delete the second creation first instead of the first.
Here is the code:
from tkinter import *
class Window:
def __init__(self, width, height):
self.root = Tk()
self.width = width
self.height = height
self.root.geometry(width + "x" + height)
class Frames:
def __init__(self):
self.l = Frame(window.root, bg="red", height=300, width=300, relief="sunken")
self.l.place(relwidth=0.3, relheight=0.3)
self.deleteB = Button(self.l, text="X", command=self.delete_frame, bg="blue")
self.deleteB.place(rely=0, relx=0.92)
self.addB = Button(self.l, text="Add", command=self.add_note, bg="blue")
self.addB.place(rely=0, relx=0.65)
def delete_frame(self):
self.l.pack()
self.l.pack_forget()
self.l.destroy()
frames.pop()
def add_note(self):
self.note_Label = Label(self.l, text="Clean the room")
self.note_Label.pack(padx=20, pady=10)
self.delete_Note = Button(self.note_Label, text="X", command=self.del_Note)
self.delete_Note.pack(padx=5, pady=5)
def del_Note(self):
self.note_Label.pack_forget()
self.note_Label.destroy()
class Note:
def __init__(self):
pass
class DragNDrop:
def __init__(self):
pass
def make_draggable(self, widget):
widget.bind("<Button-1>", self.on_drag_start)
widget.bind("<B1-Motion>", self.on_drag_motion)
def on_drag_start(self, event):
widget = event.widget
widget._drag_start_x = event.x
widget._drag_start_y = event.y
def on_drag_motion(self, event):
widget = event.widget
x = widget.winfo_x() - widget._drag_start_x + event.x
y = widget.winfo_y() - widget._drag_start_y + event.y
widget.place(x=x, y=y)
class Buttons:
def __init__(self):
self.button = Button(window.root, width=20, height=20, bg="blue", command=self.add_frames)
self.button.pack()
def add_frames(self):
frames.append(Frames())
print(frames)
window = Window("800", "600")
frames = []
drag = DragNDrop()
button = Buttons()
while True:
for i in frames:
drag.make_draggable(i.l)
window.root.update()
If someone has an Idea or workaround that would be nice to know.
Also I have another Idea instead of destroying them I could just hide them but in the end that makes the programm really slow at some point.
Here is the error: _tkinter.TclError: bad window path name ".!frame2"
Your code needs to remove the frame from the list. Instead, you're calling pop which always removes the last item. That causes you to lose the reference to the last window, and one of the references in frames now points to a window that has been deleted (which is the root cause of the error)
Instead, call remove:
def delete_frame(self):
self.l.destroy()
frames.remove(self)

TkInter - Can't Get Frames to work correctly and resize

TkInter's frames are driving me crazy. My goal is to have an options frame where I can select some options, then press "Archive" and the TkInter window changes to showing the output from the rest of my script.
I cannot get this to size correctly - there appears to be some additional frame taking up space in the window.
import string
from tkinter import *
import tkinter as tk
import threading
def main(argv):
print("In Main")
for arg in argv:
print(arg)
class TextOut(tk.Text):
def write(self, s):
self.insert(tk.CURRENT, s)
self.see(tk.END)
def flush(self):
pass
class Mainframe(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = OptionsFrame(self)
self._frame.pack(expand=True)
def change(self, frameClass):
# make new frame - for archive output
self._frame = frameClass(self)
self._frame.pack(fill="both", expand=True)
return self._frame
class Mainframe(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = OptionsFrame(self)
self._frame.pack(expand=True)
def change(self, newFrameClass):
# make new frame - for archive output
self._frame = newFrameClass(self)
self._frame.pack(fill="both", expand=True)
return self._frame
class OptionsFrame(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
master.title("Test")
master.geometry("325x180")
self.selectedProject = None
self.initUI(master)
def initUI(self, master):
frame1 = Frame(master)
frame1.pack(fill=BOTH, expand=True)
self.label1 = Label(frame1, text="Select Project to Archive, then click Archive")
self.projectListbox = tk.Listbox(frame1, width=60, height=100)
self.projectListbox.bind("<<ProjectSelected>>", self.changeProject)
# create a vertical scrollbar for the listbox to the right of the listbox
self.yscroll = tk.Scrollbar(self.projectListbox,command=self.projectListbox.yview,orient=tk.VERTICAL)
self.projectListbox.configure(yscrollcommand=self.yscroll.set)
# Archive button
self.archiveBtn=tk.Button(frame1,text="Archive",command=self.ArchiveButtonClick)
# Do layout
self.label1.pack()
self.projectListbox.pack(fill="both", expand=True)
self.yscroll.pack(side="right", fill="y")
self.archiveBtn.pack(side="bottom", pady=10, expand=False)
choices = ["test 1", "test 2", "test 3", "test 4", "test 5", "test 6"]
# load listbox with sorted data
for item in choices:
self.projectListbox.insert(tk.END, item)
def getSelectedProject(self):
# get selected line index
index = self.projectListbox.curselection()[0]
# get the line's text
return self.projectListbox.get(index)
# on change dropdown value
def changeProject(self,*args):
self.selectedProject = self.getSelectedProject()
def ArchiveButtonClick(self):
# Switch to second frame - for running the archive
self.changeProject(None)
# Hide existing controls
self.label1.pack_forget()
self.projectListbox.pack_forget()
self.yscroll.pack_forget()
self.archiveBtn.pack_forget()
newFrame = self.master.change(ArchivingOutputFrame)
newFrame.args = [ "-n", self.selectedProject]
newFrame.start()
# Frame shown while archive task is running
class ArchivingOutputFrame(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
master.title("Test Frame 2")
master.geometry("1000x600")
# Set up for standard output in window
self.var = tk.StringVar(self)
lbl = tk.Label(self, textvariable=self.var)
#lbl.grid(row=0, column=0)
lbl.pack(anchor="nw")
def start(self):
t = threading.Thread(target=self.process)
t.start()
def process(self):
main(self.args)
if __name__=="__main__":
# If command line options passed - skip the UI
if len(sys.argv) > 1:
main(sys.argv[1:])
else:
app=Mainframe()
text = TextOut(app)
sys.stdout = text
sys.stderr = text
text.pack(expand=True, fill=tk.BOTH)
app.mainloop()
Here is what I get in the UI; note this is showing the UI hierachy from Microsoft's Spy++ - there is a frame I didn't create (at least I don't think I did) that is at the bottom of the window and taking up half of the UI area; this is the yellow highlight. My options pane is thus squeezed into the top half.
Resize also doesn't work - if I resize the window, I get this:
When I click the button and the code to remove the options frame and put in the frame that is capturing stdout/stderr from the main script runs, I get this:
Now the extra space appears to be at the top!
Thanks for any ideas - I know I could switch to using the "Grid" UI layout engine, but this seems so simple - I'm not doing anything sophisticated here that shouldn't work with pack.
That was a lot of complicated code. It would be easier to debug if you provide a Minimal, Complete, and Verifiable example.
However; the bottom Frame is the TextOut() widget that you pack after Mainframe():
if __name__=="__main__":
app = Mainframe()
text = TextOut(app) # This one
sys.stdout = text
sys.stderr = text
text.pack(expand=True, fill=tk.BOTH)
app.mainloop()
You'll have an easier time debugging if you give each widget a bg colour and then give them all some padding so you can easier identify which widget is inside which widget.

It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console (zeromq v4.1.3)

Anaconda3-2.4.0-Windows-x86_64 install including Python 3.5 64-bit and Spyder 2.3.7.
Windows 7 Professional, SP1, 64 bit.
I am trying to follow a series of videos on youtube to add a matplotlib graph to a tkinter window, the first 5 videos cover the initial creation of windows and buttons in the tkinter window which was all fairly simple. The problem occurs when i get to the first matplotlib addition. Even though i copy exactly what the guy in the video does within Python, i get an error where he gets a working program.
Here is a link to the video i am currently attempting to follow:
How to add a Matplotlib Graph to Tkinter Window in Python 3 - Tkinter tutorial Python 3.4 p. 6
There is a list of the other videos in the series to the right when playing.
The error message given within the ipython console is: "It seems the kernel died unexpectedly. Use 'Restart kernel' to continue using this console". The message repeats every few seconds untill 'restart kernel' is selected.
In the code below if it is run as is, no windows are opened. If 'canvas.show()' is commented out, a window opens and a python crash error is shown in Windows.
the program runs and the buttons all function correctly if 'canvas.show()', 'canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand = True)' and 'canvas._tkcanvas.pack()' are all commented out.
I have researched this and found the zeromq issue with versions 4.0.6 and 4.1.1 which were fixed in the newer versions. Using the 'conda list' command in the anaconda prompt shows the version number 4.1.3, therefore i do not believe this to be the issue.
I am running in python 3.5 where 'sentdex' (the guy in the video) is running 3.4, could this cause the issue?
Here is my code (minus 3 classes which open windows with buttons on):
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk
from tkinter import ttk
LARGE_FONT = ("Arial, 12")
class Home(tk.Tk):
def __init__(self, *args):
tk.Tk.__init__(self, *args)
tk.Tk.iconbitmap(self, default="icon1.ico")
tk.Tk.wm_title(self, "Converters & Calculators")
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_columnconfigure(0, weight=1)
container.grid_rowconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, MPL_Tutorial):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Please select a calculator or convertor from the list:", font = LARGE_FONT)
label.pack(pady=20, padx=20)
MPL_Tutorial_button = ttk.Button(self, text = "Graph Page", command=lambda: controller.show_frame(MPL_Tutorial))
MPL_Tutorial_button.pack()
class MPL_Tutorial(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Graph Page", font = LARGE_FONT)
label.pack(pady=20, padx=20)
Home_button = ttk.Button(self, text = "Home", command=lambda: controller.show_frame(StartPage))
Home_button.pack()
f = Figure(figsize = (5,5), dpi=100)
a = f.add_subplot(111)
a.plot([1,2,3,4,5,6,7,8], [5,8,1,3,7,4,9,5])
canvas = FigureCanvasTkAgg(f, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand = True)
toolbar = NavigationToolbar2TkAgg(canvas, self)
toolbar.update()
canvas._tkcanvas.pack()
app = Home()
app.mainloop()
I have edited some of the content while writing the program to reflect the application i eventually want the program for, a calculator / converter for various engineering bits and pieces, such as 'reflection coefficient / VSWR ratio / Return loss conversion'. I believe the only things i have edited are names of things and some padding values.
Any help will be greatly appreciated! I fully expect the answer to be something i have done wrong, i am fairly new to programming as a whole.
Edit: Removed sections of code which referred to the other code i left out as this would undoubtedly cause 'extra' issues if anyone tried to run it.

Resources