我剛開始使用tkinter,感覺很好,但我現在面臨一個問題,似乎找不到任何關于它的資訊。我目前正在嘗試用PanedWindow():
########|#######
# | #
--------|-------
# | #
########|#######
我目前有一個代碼可以創建這個形狀,我可以調整窗格 windows 的大小(- & |)。問題是我需要這些(|)像這樣一起移動(-)。如果這還不清楚,我正在嘗試將中心分隔線移到一起。目前,他們是這樣的:
###|############
# | #
----------------
# | #
############|###
如果這還不清楚,那就試試吧!我把代碼放在下面。如您所見,您可以將垂直線移動到一起,但不能將水平線移動到一起。我知道原因是在一個主窗格視窗中有 2 個獨立的窗格視窗,因此它們獨立運行。我仍然不知道如何將它們連接在一起!
from tkinter import *
master = Tk()
master.geometry("400x400")
# Main Pane
main_panel = PanedWindow(orient=VERTICAL, bd=2, relief="solid", bg="black")
main_panel.pack(fill="both", expand=1)
# Sub Panes
##############################################################################
top_left = PanedWindow(main_panel, bd=1, relief="solid", bg="black")
top_left.pack(fill="both", expand=1)
left_label = Label(top_left, text= "Left-top")
top_left.add(left_label)
top_right = PanedWindow(top_left, orient=VERTICAL, bd=4)
top_left.add(top_right)
right_label = Label(top_left, text= "Right-top")
top_right.add(right_label)
#############################################################
bottom_left = PanedWindow(main_panel, bd=1, relief="solid", bg="black")
bottom_left.pack(fill="both", expand=1)
left_label = Label(bottom_left, text= "Left-bottom")
bottom_left.add(left_label)
bottom_right = PanedWindow(bottom_left, orient=VERTICAL, bd=4)
bottom_left.add(bottom_right)
right_label = Label(bottom_left, text= "Right-bottom")
bottom_right.add(right_label)
main_panel.add(top_left)
main_panel.add(bottom_left)
mainloop()
uj5u.com熱心網友回復:
我試圖找到這個問題的最通用的解決方案,因為我認為您可能想要向 main_panel 添加更多的窗格視窗。無論如何,這可以用更少的代碼行來實作,但為了演示,我做了比需要更多的步驟。
參考:
- 系結級別
- PanedWindow 的方法
- 事件物件
- 通用小部件方法
import tkinter as tk #dont use wildcard imports
def onB1Motion(event):#bindings are executed with event objects
widget = event.widget #get widget/paned window of event
x,y = event.x,event.y #get x and y coord of event
data = widget.identify(x,y) #paned window identify
if data != '': #identify returns empty string if child window
idx = data[0] #get sash index
orient = event.widget['orient'] #check for orientation of paned window
for child in widget.master.winfo_children(): #get all children of master
if isinstance(child,tk.PanedWindow): #if children is paned window do..
if child['orient'] == orient: #if child paned window is same orient as event paned do..
child.sash_place(idx,x,y) #places the sash with same index on same position
LABEL_WIDTH = 10 #simulate natural size by label width
master = tk.Tk()
master.geometry("400x400")
# Main Pane
main_panel = tk.PanedWindow(orient=tk.VERTICAL, bd=2, relief="solid", bg="black")
main_panel.pack(fill="both", expand=1)
# Sub Panes
##############################################################################
top_left = tk.PanedWindow(main_panel, bd=1, relief="solid", bg="black")
top_left.pack(fill="both", expand=1)
left_label = tk.Label(top_left,width=LABEL_WIDTH, text= "Left-top")
top_left.add(left_label)
top_right = tk.PanedWindow(top_left, orient=tk.VERTICAL, bd=4)
top_left.add(top_right)
right_label = tk.Label(top_left,width=LABEL_WIDTH, text= "Right-top")
top_right.add(right_label)
##############################################################################
bottom_left = tk.PanedWindow(main_panel, bd=1, relief="solid", bg="black")
bottom_left.pack(fill="both", expand=1)
left_label = tk.Label(bottom_left,width=LABEL_WIDTH, text= "Left-bottom")
bottom_left.add(left_label)
bottom_right = tk.PanedWindow(bottom_left, orient=tk.VERTICAL, bd=4)
bottom_left.add(bottom_right)
right_label = tk.Label(bottom_left,width=LABEL_WIDTH, text= "Right-bottom")
bottom_right.add(right_label)
##############################################################################
main_panel.add(top_left)
main_panel.add(bottom_left)
##binding class/all paned windows to execute command {onB1Motion} by {B1-Motion}
## USE ADD= TO DONT OVERWRITE THE STANDARD ONE
master.bind_class(main_panel.winfo_class(),'<Button1-Motion>',onB1Motion,add=' ')
master.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/357564.html
