4

I am working on a countdown timer for Ubuntu using Python and tkinter.
I have created most of the parts and now I want my app to be able run without appearing in Unity panel or Alt-Tab switching sequence. Is there any way to do this?

And also I'd like to whether it is possible to create a moveable window without the title bar. I tried root.overrideredirect(1).
But with it I am unable to move the window.

Here's the code for my program.

#!/usr/bin/python3

import tkinter as tk
from tkinter import ttk
from tkinter import TOP,LEFT
import time
import datetime
import sys





class Countdown(ttk.Frame):

    def __init__(self,parent=None, endDate=None):
        ttk.Frame.__init__(self,parent)
        self.style = ttk.Style()
        self.style.theme_use("clam")
        self.pack()
        endDate = endDate.split("/")
        self.endTime = datetime.datetime(int(endDate[2]),int(endDate[1]),int(endDate[0]))
        self.setWidgets()
        self.initWidgets()


    def setWidgets(self):
        self.dLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
        self.dLbl.pack(padx=10,pady=10,side=LEFT)
        self.hLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
        self.hLbl.pack(padx=10,pady=10,side=LEFT)
        self.mLbl = ttk.Label(self,text="0",font="Ubuntu 14 bold")
        self.mLbl.pack(padx=10,pady=10,side=LEFT)

    def getTimeDelta(self):
        self.curDate = datetime.datetime.now()
        self.diff = self.endTime - self.curDate

        self.tSec = self.diff.total_seconds()
        self.days = self.diff.days
        h = int(((self.tSec) - self.days*24*60*60)/3600)
        self.hours = h if h>0 else 0
        m = int(((self.tSec) - (self.hours*60*60+self.days*24*60*60))/60)
        self.minutes = m if m>0 else 0
        self.sec = int(self.tSec - self.minutes*60)
        return [self.days,self.hours,self.minutes+1]

    def initWidgets(self):
        def set():
            dhm = self.getTimeDelta()
            self.dLbl["text"]=str(dhm[0])+" Days"
            self.hLbl["text"]=str(dhm[1])+" Hours"
            self.mLbl["text"]=str(dhm[2])+" Mins"
            self.after(1000,set)
        set()

root = tk.Tk()
root.title(sys.argv[1])
app = Countdown(root, sys.argv[2])
app.mainloop()
Isuru Pathirana
  • 1,060
  • 1
  • 16
  • 37

3 Answers3

0

To move a window without borders you can take a look at this question for a simple example of how to implement what you want and just keep building on top of it.

For the hiding, you could use .iconify(), theoretically minimizing the app to the tray thus hiding it, and .deiconify(), for example:

root = tk.Tk()
root.iconify()

ps. If it don't work on Ubuntu/Unity you may have to use other GUI framework with support for this behavior on Ubuntu, like GTK.

Community
  • 1
  • 1
0
import tkinter    

root = tkinter.Tk()
root.withdraw()
root.mainloop()

Will hide the window.

Dmytro
  • 114
  • 4
0

Make your own titlebar, like so:

import sys
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk() #create the window
titlebar = tk.Label(root,height=2, bg='cyan', fg='navyblue', text=sys.argv[0]) #create the titlebar
resizable = ttk.Sizegrip(root) #make the window resizable
titlebar.pack(fill='both') # display the titlebar
root.overrideredirect(1) #make the window run without appearing in alt tab
#root.withdraw() 
#root.deiconify()
root.geometry('200x200') #set the window size to 200x200
resizable.pack(fill='y',side='right')
def ontitlebarclicked(event):
    global lastposition
    lastposition=[event.x,event.y] 

def ontitlebardragged(event):
    global lastposition
    windowposition=[int(i) for i in root.geometry().split('+')[1:]] # get the window position
    diffposition=[event.x-lastposition[0],event.y-lastposition[1]]
    widthheight=root.geometry().split('+')[0]
    root.geometry(widthheight+'+'+str(windowposition[0]+diffposition[0])+'+'+str(windowposition[1]+diffposition[1]))

titlebar.bind('<Button-1>',ontitlebarclicked)
titlebar.bind('<Button1-Motion>',ontitlebardragged)
titlebar.focus_force()

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
guest
  • 1