【问题标题】:Tkiner, The frames are located in the middleTkinter,框架位于中间
【发布时间】:2020-05-19 12:42:50
【问题描述】:

我目前正在制作一个信使程序。正如您在标题中看到的那样,我使用 Tkinter 在框架内创建了一个框架,并且内部框架自动垂直对齐。但我不想要这个。他们总是想从头开始。框架内有大量框架时是可以的,但如果没有,就会出现问题。当然,这是因为我对'Tkinter'缺乏了解,但我还是请你帮忙。我认为这是类 List 中的框架或框架的 '' 事件的问题

并用代码向韩语道歉

import tkinter as tk
from inspect import *


memberInfo = [(1, 'ukhyun', '1234', '하욱현', '01024021051', 'hyun4911@gmail.com', 'ukhyun.png'),
                (2, 'ukhyun1', '1234', '하욱현1', '01024021051', 'hyun4911@gmail.com', 'ukhyun1.png'),
                (3, 'ukhyun2', '1234', 'Jail', '01024021051', 'hyun4911@gmail.com', 'jail.png'),
                (4, 'ukhyun', '1234', 'welcome', '01024021051', 'hyun4911@gmail.com', 'welcome.png'),
                (5, 'ukhyun3', '1234', 'hello', '01024021051', 'hyun4911@gmail.com', 'hello.png'),
                (6, 'ukhyun', '1234', 'welcome', '01024021051', 'hyun4911@gmail.com', 'welcome.png'),
                (7, 'ukhyun3', '1234', 'hello', '01024021051', 'hyun4911@gmail.com', 'hello.png'),
                (8, 'ukhyun', '1234', 'welcome', '01024021051', 'hyun4911@gmail.com', 'welcome.png'),
                (9, 'ukhyun3', '1234', 'hello', '01024021051', 'hyun4911@gmail.com', 'hello.png'),
                (10, 'ukhyun', '1234', 'welcome', '01024021051', 'hyun4911@gmail.com', 'welcome.png'),
                (11, 'ukhyun3', '1234', 'hello', '01024021051', 'hyun4911@gmail.com', 'hello.png'),
                (12, 'ukhyun', '1234', 'welcome', '01024021051', 'hyun4911@gmail.com', 'welcome.png'),
                (13, 'ukhyun3', '1234', 'hello', '01024021051', 'hyun4911@gmail.com', 'hello.png'),
                (14, 'ukhyun', '1234', 'welcome', '01024021051', 'hyun4911@gmail.com', 'welcome.png'),
                (15, 'ukhyun3', '1234', 'hello', '01024021051', 'hyun4911@gmail.com', 'hello.png'),
                (16, 'ukhyun', '1234', 'welcome', '01024021051', 'hyun4911@gmail.com', 'welcome.png'),
                (17, 'ukhyun3', '1234', 'hello', '01024021051', 'hyun4911@gmail.com', 'hello.png'),
                (18, 'ukhyun', '1234', 'welcome', '01024021051', 'hyun4911@gmail.com', 'welcome.png'),
                (19, 'ukhyun3', '1234', 'hello', '01024021051', 'hyun4911@gmail.com', 'hello.png')]

class mainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwagrs):
        tk.Frame.__init__(self, parent, *args, **kwagrs)
        self.parent = parent
        self.config(bg="white")

        staff = staffList(self)


class List(tk.Frame):
    def __init__(self, parent, *args, **kwagrs):
        tk.Frame.__init__(self, parent, *args, **kwagrs)
        self.config(bg='red')
        self.pack(fill='both', expand=True)

        self.vScrollBar = tk.Scrollbar(self, orient=tk.VERTICAL)
        self.vScrollBar.pack(fill='y', side='right')

        self.canvas = tk.Canvas(
            self, bg='blue', bd=0, highlightthickness=0, yscrollcommand=self.vScrollBar.set)
        self.canvas.pack(fill='both', side='left', expand=True)

        self.vScrollBar.config(command=self.canvas.yview)

        self.interior = tk.Frame(self.canvas, bg="black")
        self.interior.pack(fill='both')

        self.interiorId = self.canvas.create_window(
            0, 0, window=self.interior, anchor='nw')

        def _configure_interior(e):
            size = (self.interior.winfo_reqwidth(),
                    self.interior.winfo_reqheight())
            self.canvas.config(scrollregion=self.interior.bbox('all'))
            print(self.canvas.yview)

            if self.interior.winfo_reqwidth() != self.canvas.winfo_width():
                self.canvas.config(width=self.interior.winfo_reqwidth())
        self.interior.bind("<Configure>", _configure_interior)

        def _configure_canvas(e):
            if self.interior.winfo_reqwidth() != self.canvas.winfo_width():
                self.canvas.itemconfigure(
                    self.interiorId, width=self.canvas.winfo_width())
        self.canvas.bind("<Configure>", _configure_canvas)

        def _wheel(e):
            self.canvas.yview_scroll(round(-1*(e.delta/120)), 'units')
        self.canvas.bind_all("<MouseWheel>", _wheel)

        for i in getmembers(self.canvas.yview):
            print(i)
        print(self.canvas.yview)


class staffList(List):
    def __init__(self, parent, *args, **kwargs):
        List.__init__(self, parent, *args, **kwargs)

        self.users = []
        self.imgs = []
        for i in range(3):
            self.users.append(tk.Frame(self.interior, bd=0, bg='red'))
            self.users[-1].pack(side='top', fill='x')

            tk.Label(self.users[-1], text="hello").pack()


if __name__ == "__main__":
    root = tk.Tk()
    root.title("Messenger - GoodAuction")
    root.minsize(400, 500)
    root.geometry('400x600+200+200')
    root.iconbitmap(default=r"./img/favicon.ico")

    app = mainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

【问题讨论】:

  • 那是很多代码,其中大部分与 tkinter 无关。您能否尝试将代码最小化,以便更轻松地为您提供帮助:)
  • 对不起读者,我在帖子中编辑了我的代码
  • 我认为self.canvas.config(scrollregion=self.interior.bbox('all')) 应该是self.canvas.config(scrollregion=self.canvas.bbox('all'))。这些标签上方的空间(蓝色区域)是由于滚动区域小于可视区域。如果您放置更多标签,例如 30 个标签,您会看到差异。

标签: python tkinter


【解决方案1】:

不确定我是否理解正确,但看看这是否正确; 您有三个垂直包装的标签:

hello
hello
hello

但希望它们是横向的:

hello hello hello

在这种情况下;在staffList() 中,你明确地垂直包装它们:

self.users[-1].pack(side='top', fill='x')
                           ^
packs vertically ----------|

但您可以将其更改为:

self.users[-1].pack(side='left', fill='x')
                           ^
packs to the left----------|

还是我误会了你?

【讨论】:

  • 感谢您先回答!这和我想要的有点不同。也许我的解释是缺乏的。抬起鼠标滚轮,您将在标签上方看到无意义的空间。我想摆脱这个空间并在上面放一个标签。
  • 再看一遍,我找不到搞砸滚动区域的东西。但是话又说回来,我真的不明白你的实现。
猜你喜欢
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多