How to show tkinter buttons in mac touch bar - python-3.x

I have a tkinter application and want to display some of its buttons in the Mac Touch Bar.
For Example
from tkinter import *
root = Tk()
send_button = Button(text = "Send",bg = 'sky blue',fg = 'black',font = "size 15",pady = 5)
send_button.grid()
root.mainloop()
So I want to integrate the send_button to show up in the Touch Bar, so is there a way to do that in tkinter.

There is a Package called PyTouchBar. I think this would help. https://github.com/Maxmad68/PyTouchBar/wiki
As an Example:
root = Tk()
PyTouchBar.prepare_tk_windows(root)
def function(button):
print('Button clicked!')
button = PyTouchBar.TouchBarItems.Button(title='Click me!', action=function)
PyTouchBar.set_touchbar([button])
root.mainloop()

Related

Create notepad via python

Scope - Python3.10
Looking for -
add multiple editable notes in tabs. (just like notepad)
How to add a save function for a specific note
enter image description here
Attaching work done till now, Help is much appreciated
from tkinter import *
from tkinter import ttk
MainWindow = Tk() #Object#
MainWindow.geometry ('380x250') #size of object#
MainWindow.title("Bnote") #Name of object#
Menubar = Menu(MainWindow)
Appearance = Menu(Menubar,tearoff=0)
NotesArea = Text(MainWindow)
def test(Menubar):
Menubar.entryconfigure(1, label="click")
file_menu = Menu(Menubar, tearoff=False)
file_menu.add_command(label="New_Tab", command=lambda: clicked(file_menu))
Menubar.add_cascade(label="+", menu=file_menu)
scrollbar = Scrollbar(MainWindow) #adding scrollbar#
scrollbar.pack(side=RIGHT,fill=Y) #packing scrollbar#
MainWindow.config(menu=Menubar)
NotesArea = Text(MainWindow,yscrollcommand=scrollbar.set,wrap = WORD, font="Ubuntu 12")
scrollbar.config(command=NotesArea.yview) # configuring scrollbar
NotesArea.pack(fill=BOTH)
MainWindow.mainloop ()

How to remove a button in a tkinter

The problem is that if I want to remove these buttons, which have been allocated space by x and y, it cannot be and does not generate any error or anything else shows as if it did not do any comedy, and when the button is normal it works as it should
import tkinter as t
root = t.Tk()
def click():
button.pack_forget()
root.maxsize(375, 912)
img = t.PhotoImage(file="background.png")
background = t.Label(root, image=img)
background.pack()
button = t.Button(root, text ="Click\nme", command=click)
button.pack()
button.place(x=157, y=600)
root.mainloop()

Tkinter Toplevel window not appearing

Python 3.8, Win 10 is the os, Toplevel widget does not appear to be working with new window not appearing. Any guidance would be appreciated, thanks!
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
top.mainloop()
my_button = Button(root, text="Popup, click here", command="popup")
my_button.grid(row=0, column=0)
root.mainloop()
Problem:
The only issue here is that the callback command shouldn't be a string.
Solution:
Remove the quotes around popup and the Toplevel window should appear.
Fixed Code:
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
my_label_top.pack()
my_button = Button(root, text="Popup, click here", command=popup)
my_button.grid(row=0, column=0)
root.mainloop()
Tips:
Using top.mainloop() is not necessary.
You also forgot to pack() the Label(my_label_top)

How to run a python tkinter app without appearing in unity panel or alt-tab

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()
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.
import tkinter
root = tkinter.Tk()
root.withdraw()
root.mainloop()
Will hide the window.
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()

Python 3 Tkinter - Messagebox with a toplevel as master?

I've found that when a toplevel widget calls a messagebox dialog (like "showinfo"), the root window is showed up, over the toplevel. Is there a way to set the Toplevel window as the master of the messagebox dialog ?
Here is a script to reproduce this :
# -*- coding:utf-8 -*-
# PYTHON 3 ONLY
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('ROOT WINDOW')
Label(root, text = 'Place the toplevel window over the root window\nThen, push the button and you will see that the root window is again over the toplevel').grid()
topWindow = Toplevel(root)
topWindow.title('TOPLEVEL WINDOW')
Label(topWindow, text = 'This button will open a messagebox but will\ndo a "focus_force()" thing on the root window').grid()
Button(topWindow, text = '[Push me !]', command = lambda: messagebox.showinfo('foo', 'bar!')).grid()
# --
root.mainloop()
You can set the parent argument to topWindow for the showInfo command:
Button(..., command=lambda: messagebox.showInfo(parent=topWindow, ...))
See also:
http://effbot.org/tkinterbook/tkinter-standard-dialogs.htm
This may address more current versions.
#TEST AREA forcommands/methods/options/attributes
#standard set up header code 2
from tkinter import *
from tkinter import messagebox
root = Tk()
root.attributes('-fullscreen', True)
root.configure(background='white')
scrW = root.winfo_screenwidth()
scrH = root.winfo_screenheight()
workwindow = str(1024) + "x" + str(768)+ "+" +str(int((scrW-1024)/2)) + "+" +str(int((scrH-768)/2))
top1 = Toplevel(root, bg="light blue")
top1.geometry(workwindow)
top1.title("Top 1 - Workwindow")
top1.attributes("-topmost", 1) # make sure top1 is on top to start
root.update() # but don't leave it locked in place
top1.attributes("-topmost", 0) # in case you use lower or lift
#exit button - note: uses grid
b3=Button(root, text="Egress", command=root.destroy)
b3.grid(row=0,column=0,ipadx=10, ipady=10, pady=5, padx=5, sticky = W+N)
#____________________________
root.withdraw()
mb1=messagebox.askquestion(top1, "Pay attention: \nThis is the message?")
messagebox.showinfo("Say Hello", "Hello World")
root.deiconify()
top1.lift(aboveThis=None)
#____________________________
root.mainloop()
Button(..., command=lambda: messagebox.showinfo("The Title", "A piece of text", parent=topWindow))
This will definitely work. The syntax of the messagebox is
messagebox.Function_Name(title, message [, options])
setting the parent argument is one of the options
add topWindow.attributes("-topmost", 1)
after defining the Toplevel, and this should fix the problem

Resources