Python Forum

Full Version: looking for an example to start with
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have been wanting to add some buttons on the Ubuntu Unity framework. people have tried to tell me how but nothing ever has made a button ever show up, much less run what i want to do. way back in time when i used Slackware i was directed to a program in C++ (not the same as C) that added a drop down menu. but i am looking for one-action buttons. so, i am thinking maybe the Python GUI department might have something to do this.

what i am looking for right now is a GUI program that will run in the background and do the following GUI thing. it will stay hidden on the right side or the bottom and be sensitive to mouse movement coming at it or touching it at the edge (the latter is probably better). when the mouse triggers it, it brings out a panel of small buttons. moving the mouse aways causes it to disappear, such as by sliding off screen. if the mouse stays, it stays. click a button and it starts a configured script or program or calls some python code added in.

what i am hoping for is a simple thing like this in Python showing how to do all the GUI setup and actions. then i can customize it with my own buttons, sizes, and click actions.
I made a small tkinter window with ten useless buttons which lowers itself to the bottom of all windows when the mouse is not near it, but raises itself when the mouse is near it. I made it update itself once a second, and so can be somewhat laggy. It works in Windows 10 (and crashed once), but I am unsure whether root.wm_attributes('-topmost', True) works in anything else. Alternate mouse position, maybe
from tkinter import *

root = Tk()
root.overrideredirect(True)
root.geometry('100x200+1250+500')
root.lift()
root.wm_attributes('-topmost', True)

B1 = Button(root, width=2, height=1, text='1')
B1.place(x=5, y=5)
B2 = Button(root, width=2, height=1, text='2')
B2.place(x=40, y=5)
B3 = Button(root, width=2, height=1, text='3')
B3.place(x=5, y=40)
B4 = Button(root, width=2, height=1, text='4')
B4.place(x=40, y=40)
B5 = Button(root, width=2, height=1, text='5')
B5.place(x=5, y=75)
B6 = Button(root, width=2, height=1, text='6')
B6.place(x=40, y=75)
B7 = Button(root, width=2, height=1, text='7')
B7.place(x=5, y=110)
B8 = Button(root, width=2, height=1, text='8')
B8.place(x=40, y=110)
B9 = Button(root, width=2, height=1, text='9')
B9.place(x=5, y=145)
B10 = Button(root, width=2, height=1, text='10')
B10.place(x=40, y=145)

L = Label(root, text='Button Box')
L.place(x=10, y=175)

def Hide():
    while 1:
        x = root.winfo_pointerx()
        y = root.winfo_pointery()

        if abs((x-1250)) >= 50 and abs((y-500)) >=100:
            root.lower()
        if abs((x-1250)) <= 50 and abs((y-500)) <=100:
            root.lift()
        root.update

root.after(1000, Hide)
root.mainloop()
if the 10 buttons do nothing then i won't be able to see how to make a button run something i want to have run when i press that button. i tried running this and the panel was out in the middle of the screen, the display, and my terminal window. i guess this will need to get info of the size of my display (1920x1080) and look at the mouse more often. is there a way to have mouse positions sent to it, instead of it looping fast to track it? can you setup a dictionary indexed by button name or number giving a command to run, and actually run it w/o waiting for it (if the command is to sleep for an hour then i could start hundreds of them by doing hundreds of clicks ... that's what i mean by not waiting for it)
from tkinter import *

root = Tk()
root.overrideredirect(True)
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
xp = sw - 116
yp = sh - 268
root.geometry('100x200+{}+{}'.format(xp, yp))
root.lift()
root.wm_attributes('-topmost', True)

def C1():
    C1 = Tk()
    C1.geometry('300x30')
    C1.lift()
    C1L = Label(C1, text='You pressed button one.')
    C1L.pack()
def C2():
    C2 = Tk()
    C2.geometry('300x30')
    C2.lift()
    C2L = Label(C2, text='You pressed button two.')
    C2L.pack()
def C3():
    C3 = Tk()
    C3.geometry('300x30')
    C3.lift()
    C3L = Label(C3, text='You pressed button three.')
    C3L.pack()
def C4():
    C4 = Tk()
    C4.geometry('300x30')
    C4.lift()
    C4L = Label(C4, text='You pressed button four.')
    C4L.pack()
def C5():
    C5 = Tk()
    C5.geometry('300x30')
    C5.lift()
    C5L = Label(C5, text='You pressed button five.')
    C5L.pack()
def C6():
    C6 = Tk()
    C6.geometry('300x30')
    C6.lift()
    C6L = Label(C6, text='You pressed button six.')
    C6L.pack()
def C7():
    C7 = Tk()
    C7.geometry('300x30')
    C7.lift()
    C7L = Label(C7, text='You pressed button seven.')
    C7L.pack()
def C8():
    C8 = Tk()
    C8.geometry('300x30')
    C8.lift()
    C8L = Label(C8, text='You pressed button eight.')
    C8L.pack()
def C9():
    C9 = Tk()
    C9.geometry('300x30')
    C9.lift()
    C9L = Label(C9, text='You pressed button nine.')
    C9L.pack()
def C10():
    C10 = Tk()
    C10.geometry('300x30')
    C10.lift()
    C10L = Label(C10, text='You pressed button ten.')
    C10L.pack()

B1 = Button(root, width=2, height=1, text='1', command=C1)
B1.place(x=5, y=5)
B2 = Button(root, width=2, height=1, text='2', command=C2)
B2.place(x=40, y=5)
B3 = Button(root, width=2, height=1, text='3', command=C3)
B3.place(x=5, y=40)
B4 = Button(root, width=2, height=1, text='4', command=C4)
B4.place(x=40, y=40)
B5 = Button(root, width=2, height=1, text='5', command=C5)
B5.place(x=5, y=75)
B6 = Button(root, width=2, height=1, text='6', command=C6)
B6.place(x=40, y=75)
B7 = Button(root, width=2, height=1, text='7', command=C7)
B7.place(x=5, y=110)
B8 = Button(root, width=2, height=1, text='8', command=C8)
B8.place(x=40, y=110)
B9 = Button(root, width=2, height=1, text='9', command=C9)
B9.place(x=5, y=145)
B10 = Button(root, width=2, height=1, text='10', command=C10)
B10.place(x=40, y=145)

L = Label(root, text='Button Box')
L.place(x=10, y=175)

def Hide():
    while 1:
        x = root.winfo_pointerx()
        y = root.winfo_pointery()

        if abs((x-xp)) >= 50 and abs((y-yp)) >=100:
            root.lower()
        if abs((x-xp)) <= 50 and abs((y-yp)) <=100:
            root.lift()
        root.update()

root.after(200, Hide)
root.mainloop()
Seems to work, and I believe that it should regardless of screen resolution. I made it have some dummy commands for the buttons, but depending on the command, you may need to use threading so that the main button tray will work still.
actually, i am wanting to start a new process, and leave it be. i know how to do that. i just to be sure i can have the button action run any code. calling a function should be fine. then i can do whatever i want, like call subprocess.Popen() with my arguments. any reason i can't do class_instance.method() calls on the Button(..., Command= ) calls?

another GUI program i want to do later on, some day, is drawing Mandelbrot fractals in a special pixel order. i will need to create a square box (probably 258x258) of pixels that i can click on and then know just where the mouse clicked at or get a single keystroke (paying attention to numbers on the keypad). i will be doing deep-deep computations, maybe with scaled ints. the rest of the window will be higher level stuff like a backup button and a done button and some text areas.