0

I've made a widget movable after using overrideredirect(1), everything is fine except that when I drag the windo left and right many times fast or drag it until the borders of my screen, when doing so the widgets it has in it are disappearing.

Here's my code:

from tkinter import *
import mouse

root = Tk()
root.overrideredirect(1)
root.geometry("700x200+500+200")

BG = Label(root, bd=0)
BG.pack(fill=BOTH, expand=True)


var1 = StringVar()

entry1 = Entry(BG, width=80, textvariable=var1)
entry1.place(x=170,y=57)

#-----------------------------------------------
var2 = StringVar()

entry2 = Entry(BG,width=80, textvariable=var2)
entry2.place(x=170,y=100)

def standard_bind():
   BG.bind('<B1-Motion>', lambda e: event(e, Mode=True))

def event(widget, Mode=False):
    global x, y
    if Mode:
        x = widget.x
        y = widget.y
    
    BG.bind('<B1-Motion>', lambda e: event(e))
    root.geometry('+%d+%d' % (mouse.get_position()[0]-x, mouse.get_position()[1]-y))


BG.bind('<B1-Motion>', lambda e: event(e, Mode=True))
BG.bind('<ButtonRelease-1>', lambda e: standard_bind())

root.mainloop()
  • 1
    why are you using the external `mouse` module? tkinter can give you the position of the pointer. – Bryan Oakley Aug 07 '21 at 19:11
  • Try using [this](https://stackoverflow.com/a/29643532/11106801) – TheLizzard Aug 07 '21 at 19:13
  • @BryanOakley I don't understand tkinter's thingy –  Aug 07 '21 at 19:16
  • @TheLizzard again, I don't really understand about classes, so I prefer to the one I'm using right now which's shown just below it ... –  Aug 07 '21 at 19:18
  • @ShadowKurgansk I can argue that it very bad coding. Also you don't need to know about classes. Just use `Win()` instead of `Tk()` in your program when using the first answer. – TheLizzard Aug 07 '21 at 19:20
  • Well, I would still be unable to add widgets on it and do many other things @TheLizzard –  Aug 07 '21 at 19:22
  • @ShadowKurgansk I edited the first answer on the link that I sent you. Look at how I created 2 labels. It is absolutely the same as just using `Tk()`. Also when you say you are *unable to add widgets* what do you mean exactly? – TheLizzard Aug 07 '21 at 19:24
  • I mean I really know nothing about classes in short, it seems very confusing to me for some reason. –  Aug 07 '21 at 19:26
  • or how do we add functions into that? Sorry for the inconvenience I have ADHD, some understanding troubles ... –  Aug 07 '21 at 19:29
  • @ShadowKurgansk Add the code that is inside the answer to the top of your application. Then every time you need to use `Tk()` to create a new window use `Win()` instead. – TheLizzard Aug 07 '21 at 19:31
  • Let me try @TheLizzard –  Aug 07 '21 at 19:42
  • @TheLizzard I did so, with almost everything I have done so far it creates an error, I added 'tk.' before every widget but now it doesn't recognize StringVar(), what should I do? –  Aug 07 '21 at 19:53
  • @ShadowKurgansk Did you add `tk.` before the `StringVar()` (it should be `tk.StringVar()`)? – TheLizzard Aug 07 '21 at 19:54
  • Oh yeah, now it's working but it has still the same issue, when I drag it around the borders of the screen the widgets are disappearing, do you wish to see the code?@TheLizzard –  Aug 07 '21 at 19:58
  • I think that issue cannot be fixed –  Aug 07 '21 at 20:00
  • @ShadowKurgansk Do you mean that the widgets are going outside of the screen or do they actually disappear? If they are disappearing, then I can't think of any possible solutions. – TheLizzard Aug 07 '21 at 20:01
  • @TheLizzard I mean you if you move the main window until the border of your PC's screen, the widgets it has in it are disappearing –  Aug 07 '21 at 20:03
  • @ShadowKurgansk That shouldn't happen. I have no idea why it might be happening. – TheLizzard Aug 07 '21 at 20:06
  • Doesn't it happening in yours? Try to drag down as you can ... @TheLizzard –  Aug 07 '21 at 20:08
  • I can drag it outside of the screen but when I drag it back inside the screen all of the widgets are still visible. – TheLizzard Aug 07 '21 at 20:10
  • @TheLizzard very weird, lol –  Aug 07 '21 at 20:17
  • @TheLizzard I have another issue, can you help me? –  Aug 07 '21 at 21:22
  • @BryanOakley I have an issue that appears when the code is full, when I delete some parts the issue disappears, can I upload the whole code? It's 191 lines, the problem should be about images too so i think i would need to upload the images aswell ... –  Aug 07 '21 at 21:36
  • @ShadowKurgansk If deleting any part of the code removes the problem, then we would consider that code minimal enough so yes you can post it. Although it's rare to see a minimal example that takes more than 70 lines of code. Also it might be worth creating a new question – TheLizzard Aug 07 '21 at 21:57
  • _"I have an issue that appears when the code is full, when I delete some parts the issue disappears"_ - then that's telling you that the problem is likely due to some of the code you removed. Please include information about which block of code that, when removed, makes the problem go away. – Bryan Oakley Aug 07 '21 at 22:26
  • @BryanOakley It's found out that weirdly any valid configuration of my custom titlebar makes the other widgets disappear ( in the code i posted before, this one is incomplete) –  Aug 07 '21 at 22:34
  • @TheLizzard if i do so can I include the needed assets with it? –  Aug 07 '21 at 22:37
  • @ShadowKurgansk Yes you can. Also look at [this](https://github.com/TheLizzard/BetterTk/blob/main/src/bettertk/win.py) if you want to see how I made a `tkinter` window with a custom titlebar. – TheLizzard Aug 08 '21 at 08:20

0 Answers0