I am trying to create a GUI front-end to display some data using python tk inter.
I have a Frame which in-turn has other widgets like, button and list-box etc. I am trying to dynamically increase/decrease the frame whenever the cursor is placed on borders of frame just like we do on usual windows from user end.
I have gone through the binding functions for the same. but looks like I am missing on something.
def left_click(event):
tkinter.Label(Frame1, text = "Left Click!").pack()
self.Frame1 = Frame(top)
self.Frame1.place(relx=0.01, rely=0.152, relheight=0.678 , relwidth=0.98)
self.Frame1.configure(relief=GROOVE)
self.Frame1.configure(borderwidth="10")
self.Frame1.configure(relief=GROOVE)
self.Frame1.configure(background="#D1C8E6")
self.Frame1.configure(width=900)
self.Frame1.configure(highlightthickness="4")
self.Frame1.bind("<Button-1>", left_click)
I am new to python tk inter.
You need to bind the following mouse events to perform frame resize:
<ButtonPress-1>
(mouse left button pressed) to determine whether resize should be started based on mouse position <ButtonRelease-1>
(mouse left button released) to stop resize <Motion>
(mouse moving within frame) to perform resize if resize is started by mouse pressed Below is a sample code:
from tkinter import *
HORIZONTAL = 1
VERTICAL = 2
class App:
def __init__(self, top):
self.Frame1 = Frame(top, bd=5, relief='raised', width=100, height=100)
self.Frame1.place(x=10, y=10)
self.Frame1.bind("<ButtonPress-1>", self.start_resize)
self.Frame1.bind("<ButtonRelease-1>", self.stop_resize)
self.Frame1.bind("<Motion>", self.resize_frame)
self.resize_mode = 0
self.cursor = ''
def check_resize_mode(self, x, y):
width, height = self.Frame1.cget('width'), self.Frame1.cget('height')
mode = 0
if x > width-10: mode |= HORIZONTAL
if y > height-10: mode |= VERTICAL
return mode
def start_resize(self, event):
self.resize_mode = self.check_resize_mode(event.x, event.y)
def resize_frame(self, event):
if self.resize_mode:
if self.resize_mode & HORIZONTAL:
self.Frame1.config(width=event.x)
if self.resize_mode & VERTICAL:
self.Frame1.config(height=event.y)
else:
cursor = 'size' if self.check_resize_mode(event.x, event.y) else ''
if cursor != self.cursor:
self.Frame1.config(cursor=cursor)
self.cursor = cursor
def stop_resize(self, event):
self.resize_mode = 0
root = Tk()
root.geometry("800x600+400+50")
App(root)
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.