52687

How to move button outside of his parent with tkinter?

Question:

I'm currently trying to move a button by using drag and drop with tkinter. The problem is that when I'm trying to move my button, it's working but I can't move it outside his parent: I have a LabelFrame which contains several LabelFrame with buttons. I'm trying to drag and drop a button from a LabelFrame to the other one but when the button is going outside of his parent, it "disappears". I'm using the method "place" of the widget to move it during my drag.

I'm not sure if my problem is really understandable. I'll put some codes if it isn't to explain it better.

Answer1:

Widgets exist in a hierarchy, and every widget will be visually clipped by its parent. Since you want a widget to appear in different frames at different times, it simply cannot be a child of either frame. Instead, make it a child of the parent of the frames. You can then use place (or pack or grid) to put the widget in either frame by using the in_ parameter.

Here's an example. It doesn't use drag and drop in order to keep the code compact, but it illustrates the principle. Click on the button to move it from one frame to the other.

import tkinter as tk class Example: def __init__(self): self.root = tk.Tk() self.lf1 = tk.LabelFrame(self.root, text="Choose me!", width=200, height=200) self.lf2 = tk.LabelFrame(self.root, text="No! Choose me!", width=200, height=200) self.lf1.pack(side="left", fill="both", expand=True) self.lf2.pack(side="right", fill="both", expand=True) self.button = tk.Button(self.root, text="Click me", command=self.on_click) self.button.place(in_=self.lf1, x=20, y=20) def start(self): self.root.mainloop() def on_click(self): current_frame = self.button.place_info().get("in") new_frame = self.lf1 if current_frame == self.lf2 else self.lf2 self.button.place(in_=new_frame, x=20, y=20) Example().start()

Recommend

  • ListView styling a selected item text-fill - JavaFX 8 MAGIC or BUG?
  • Include Adorner in ScrollViewer scrollable region
  • Sections with unusual borders
  • Do tracking pixels need to have alt attributes for accessibility (WCAG 2.0)?
  • searchBar from UISearchController not showing correctly on split view on iPad
  • Managing NSNotification along with UITabBarController interactions
  • Dendrogram or Other Plot from Distance Matrix
  • NHibernate Component inheritance
  • Is it good practice to put Edge Side Includes into my templates?
  • Can't figure out a function to return a reference to a given type stored in RefCell
  • k-ary Trees in Java
  • Starting Android AVD crashes with java.awt.HeadlessException
  • Android Oreo JobIntentService Keep running in background for Android 7 &below and crashing often
  • How to resize image if the image is bigger than Textbox.But it not increase scale if smaller
  • When interface inheritance in Java is useful?
  • Multilingual set up of codeigniter
  • Is there an HTML code that can make my background picture transparent and my text non-transparent?
  • How to start server for Selenium grid Java Maven setup
  • Redirect to trailng slash (htaccess)
  • Allocating a 2D contiguous array within a function
  • How to make nicEditor snaplet? (Several questions)
  • Setting src to Base64-encoded image with Javascript is failing
  • @tailrec why does this method not compile with 'contains a recursive call not in tail position&
  • remove unicode characters but keep all special and English characters with preg_replace
  • Is there a Windows socket API call / option to “block” a range of ports à la SO_EXCLUSIVEADDRUSE
  • Dynamically load css stylesheet and wait for it to load
  • PayPal API Listener Website Payments Standard URI
  • SQL Query - Table Joining Problems
  • Python ImageIO Gif Set Delay Between Frames
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • Zurb Foundation _global.scss meta styles for js?
  • Xcode 4 NSLog Macro link in Xcode 3
  • how to adjust image in a panel in Java swing?
  • How do I change content of ComboFieldEditor?
  • Update CALayer sublayers immediately
  • TFS: Get latest causes slow project reloading
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize