使用Python tkin创建可滚动、可包装的顶层

2024-04-19 22:46:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想说,我对使用tkinter和Python进行编码非常陌生。希望你能帮我。你知道吗

我正在尝试创建一个应用程序,其中包含一个帮助部分,当单击按钮时,该部分应该作为tkinter的顶层打开。顶层需要滚动和包装的文本,因为我调整了顶层窗口。你知道吗

下面是一个工作代码,它给了我一个很好的滚动窗口,但不会包装文本。底部的Expl(300300)是从另一个Python文件调用的原始设置,该文件有一个maintk。tk()-窗口。有谁能给我一些建议,告诉我包装功能是如何工作的,以及我现在做错了什么?你知道吗

import tkinter as tk

WIDTH = 500
HEIGHT = 500
BG = '#fff'

class Expl(tk.Frame):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = round(WIDTH/2)
        self.height = round(HEIGHT/1.8)

        self.window = tk.Toplevel()

        self.window.wm_geometry('%dx%d+%d+%d' % (self.width, self.height, max(0, self.x - (WIDTH/2 - WIDTH/2/2)), max(0, self.y - (HEIGHT/2 - HEIGHT/1.8/2))))
        self.window.title('Förklaringar')
        self.window.configure(background=BG)
        #self.window.iconbitmap(default=ICON_PATH)

        tk.Frame.__init__(self, self.window)
        myframe=tk.Frame(self.window, width=self.width, height=self.height, bd=1, background=BG)
        myframe.place(x=10,y=10)

        self.canvas = tk.Canvas(self.window, borderwidth=0, background=BG)        
        self.frame = tk.Frame(self.canvas, background=BG)
        self.vsb = tk.Scrollbar(self.window, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)
        self.canvas.bind_all("<MouseWheel>", self.onmousewheel)

        self.frame.bind("<Configure>", self.myfunction)
        self.frame.bind("<Configure>", self.onFrameConfigure)

        self.vsb.pack(side="right", fill="y")
        self.canvas.create_window((0,0),window=self.frame,anchor='nw')
        self.canvas.pack(side="left", fill="both", expand=True)

        self.window.grid_rowconfigure(0, weight=1)
        self.window.grid_columnconfigure(0, weight=1)

        self.draw()

    def myfunction(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def onmousewheel(self, event):
        self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")

    def onFrameConfigure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def draw(self):
        # Titles
        tk.Label(self.frame, text='TEST 1').grid(row=0, column=0, sticky='SW', padx=10, pady=(5, 0))
        tk.Label(self.frame, text='TEST 2').grid(row=1, column=0, sticky='SW', padx=10, pady=(5, 0))
        tk.Label(self.frame, text='TEST 3').grid(row=2, column=0, sticky='SW', padx=10, pady=(5, 0))

        texxt = WrappingLabel(self.frame, text="TESTTTTTT TTTTTTTT T IN RSG RSG SRG RSG")
        texxt.grid(row=3, column=0, sticky='SW', padx=10, pady=(5, 0))

class WrappingLabel(tk.Label):
    def __init__(self, master=None, **kwargs):
        tk.Label.__init__(self, master, **kwargs)
        self.bind('<Configure>', lambda _: self.config(wraplength=master.winfo_width()))

Expl(300, 300)

Tags: selfinitdefwindowwidthframelabeltk
1条回答
网友
1楼 · 发布于 2024-04-19 22:46:56

Comment: seems to be working with one line of text at least.

假设您只使用class WrappingLabel,您必须在Frame中永远.children。你知道吗

        # self.texxt.on_configure(event)
        w = self.frame
        for c in w.children:
            w.children[c].on_configure(event)

Comment: Only thing is that it is centering the 2nd line of text instead of left adjusting.

更改为:

class WrappingLabel(tk.Label):
    def __init__(self, master=None, **kwargs):
        tk.Label.__init__(self, master, justify='left', anchor='nw', **kwargs)

Question: wrap the text as I resize the Toplevel window.

您必须绑定到Canvas小部件的<Configure>事件,因为此小部件的大小与Toplevel同步。你知道吗


     on startup                   resized large                resized small 

on startupresized largeresized small

Note: For demonstration: self.frame = tk.Frame(..., bg='blue', Label(..., bg='yellow'


Note: Inherit from tk.Toplevel instead from tk.Frame.
You are using:

class Expl(tk.Frame):
    def __init__(self, x, y):
        self.window = tk.Toplevel()
        tk.Frame.__init__(self, self.window)

if you are knowing, what you do, it's ok, but the default should read:

class Expl(tk.Toplevel):
    def __init__(self, parent, x, y):
        super().__init__(parent)
        self.title('Förklaringar')

Wrap the text as the Toplevel window gets resized:

class Expl(tk.Frame):
    def __init__(self, x, y):
        self.window = tk.Toplevel()
        ...
        self.canvas = tk.Canvas(self.window, borderwidth=0, background=BG)
        self.canvas.bind("<Configure>", self.on_canvas_configure)

    def on_canvas_configure(self, event):
        # Here goes other configure for Scrollbar
        self.texxt.on_configure(event)

    def draw(self):
        ...
        self.texxt = WrappingLabel(self.frame, 
                                   text="TESTTTTTT TTTTTTTT T IN RSG RSG SRG RSG", 
                                   bg='yellow')
        ...


class WrappingLabel(tk.Label):
    def __init__(self, master=None, **kwargs):
        tk.Label.__init__(self, master, **kwargs)
        # No bind here: self.bind('<Configure>'

    def on_configure(self, event):
        widget = event.widget
        # Only on Canvas event set 'wraplength'
        if isinstance(widget, tk.Canvas):
            width = widget.winfo_width()
            # print('on_configure({})'.format((event.widget, width)))
            border = 4
            scrollbar = 12
            self.config(wraplength=width - (border + scrollbar))

用Python测试:3.5-'TclVersion':8.6'TkVersion':8.6

相关问题 更多 >