Python Forum
Losing mouse acces, Tkinter, (gui inside) System-taskbar.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Losing mouse acces, Tkinter, (gui inside) System-taskbar.
#1
I have this little tkinter information bar (window without title-bar -> auto on-top), which can be moved around with the mouse.
Everything works fine. Including, for example, a bit of sticky behavior at the screen borders.

There is just one little hiccup ...

If I move that info-bar, which has a small height, inside the system-taskbar (at bottom: Linux, Mint) I can't drag it out anymore (it is still visible though).
Figure the system-taskbar, which of course also has some on-top state is now blocking access(mouse clicks at least) to that info-bar.

Is there any way to not lose mouse access to that info-bar in this particular case ?
(probably not I figure, for multiple reasons.)

Figure I could potential add some keybind to move that info-bar to some default/save location. But if not needed ...
Preventing the info-bar from moving into the system-taskbar area is not on my mind. ... It kinda looks nice there (+out of the way of any other stuff).

(Currently I have no cleaned up test/example case(See next post) ... (Still) Seems more a general thing than potentially related to my used (basic) code.)
Reply
#2
In case it matters.
## /usr/bin/python3
import tkinter
class Tk_Test_Window():

	def __init__(self):
		self._tick_speed = 1000 ## one second -- general Tk wait/sleep time.
		self._gui_setup()

	def _gui_setup(self):

		self.root = tkinter.Tk()
		screenWidth = self.root.winfo_screenwidth()

		## gui setup.
		self.root.title("Test Window")
		self.root.geometry("100x50+"+str(int(screenWidth/2)+100)+"+0")
		self.root.resizable(False, False)
		if 1: self.root.overrideredirect(True) ## no title bar.
		else: self.root.attributes('-topmost', 1)

		## Move window with mouse parts.
		def SaveLastClickPos(event):
			global lastClickX, lastClickY
			lastClickX = event.x
			lastClickY = event.y
		def moveMouseButton(e):
			x = e.x - lastClickX + self.root.winfo_x()
			y = e.y - lastClickY + self.root.winfo_y()
			self.root.geometry("+%s+%s" % (x,y)) ## update gui position.
		## Move window with mouse: setup.
		self.root.bind('<Button-1>', SaveLastClickPos) ## specific for bypassing inconsistand mouse pointer jumping.
		self.root.bind("<B1-Motion>", moveMouseButton)

		self.root.wait_visibility(self.root)
		self._tick()
		self.root.mainloop()
		self.root.quit()

	def _tick(self):
		self.root_after_id = self.root.after(self._tick_speed, self._tick)

if (__name__ == '__main__'):
	htk = Tk_Test_Window()
Reply
#3
Get the screen dimensions when starting. When dragging the window, don't set y within some number of pixels of the height of the screen.
import tkinter as tk


class TestWindow(tk.Tk):

    def __init__(self):
        super().__init__()
        frame = tk.Frame(self)
        frame.pack(side=tk.TOP)
        tk.Label(frame, text="Drag me around", font=(None, 30)).pack(side=tk.LEFT)
        button = tk.Button(frame, text="x", borderwidth=0, command=self.destroy)
        button.pack(anchor="ne")
        self.pos_label = tk.Label(self)
        self.pos_label.pack(side=tk.TOP)
        self.resizable(False, False)
        self.overrideredirect(True)
        # Limit where window can be dragged.
        self._max_x = self.winfo_screenwidth() - 50
        self._max_y = self.winfo_screenheight() - 100
        self._x = 0
        self._y = 0
        self.bind("<Button-1>", self._click)
        self.bind("<B1-Motion>", self._drag)

    def _click(self, event):
        """Remember mouse position when button clicked."""
        self._x = event.x
        self._y = event.y

    def _drag(self, _):
        """Reposition window to follow mouse movement."""
        x, y = self.winfo_pointerxy()
        x = min(self._max_x, x - self._x)
        y = min(self._max_y, y - self._y)
        self.pos_label["text"] = f"Position = {x}, {y}"
        self.geometry(f"+{x}+{y}")


TestWindow().mainloop()
Reply
#4
_drag() with fixed typical tk mouse jumping (on drag start, depending where you initially click).
    def _drag(self, e):
        """Reposition window to follow mouse movement."""
        x = min(self._max_x, e.x - self._x + self.winfo_x())
        y = min(self._max_y, e.y - self._y + self.winfo_y())
        self.pos_label["text"] = f"Position = {x}, {y}"
        self.geometry(f"+{x}+{y}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - touchscreen, push the button like click the mouse John64 5 5,103 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Using Tkinter inside function not working Ensaimadeta 5 10,826 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 7,345 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 4,403 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  [Tkinter] window full screen w/ taskbar at top... 3python 0 3,445 Aug-04-2021, 09:06 PM
Last Post: 3python
  [Kivy] Acces atributes from a button defined in a class inside the .kv Tomli 2 3,702 Jun-10-2021, 01:05 AM
Last Post: Tomli
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 4,479 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  TKinter coordinate system (canvas) Bhoot 1 6,902 Mar-23-2020, 04:30 PM
Last Post: Larz60+
  [PyQt] Ubuntu Taskbar icon shows title as "Unknown" while hover davidmuni 3 5,504 Jan-28-2020, 01:13 PM
Last Post: davidmuni
  Python Application in Taskbar constantin01 3 9,627 Jan-24-2020, 10:57 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020