简体   繁体   English

使用tkinter的画布框架布局

[英]frames layout with canvas using tkinter

I'm added canvas and a scroll bar to one of the frames in my script. 我在脚本的其中一个框架中添加了画布和滚动条。 However somethings wrong cause the scroll bar is off (lower bottom is not visible) and the text I drew is off. 但是出了点问题,导致滚动条关闭(下下部不可见)并且我绘制的文本关闭。 Could anyone please tell me whats the problem ? 谁能告诉我是什么问题? I want the canvas to fill the whole frame (obviously without the scroll bar) 我希望画布填充整个框架(显然没有滚动条)

import sys
import os

if sys.version_info[0] < 3:
    import Tkinter as tk
    import ttk as ttk
else:
    import tkinter as tk
    import tkinter.ttk as ttk

#
# LeftMiddle
#
class LeftMiddle(tk.Frame):
    def __init__(self, master=None):
        self.parent = master
        tk.Frame.__init__(self, self.parent, bg='bisque', borderwidth=1, relief="sunken")
        self.__create_layout()
        self.draw_text()

    def __create_layout(self):
        self.canvas = tk.Canvas(self, bg="green", relief=tk.SUNKEN)
        self.canvas.config(width=20, height=10)
        self.canvas.config(highlightthickness=0)

        self.sbar = tk.Scrollbar(self, orient=tk.VERTICAL)

        self.sbar.pack(side=tk.RIGHT, fill=tk.Y)
        self.canvas.pack(side=tk.LEFT, expand="YES", fill=tk.BOTH)

    def draw_text(self):    
        self.canvas.create_text(0, 0, text='1234567890', fill='red')
        self.canvas.create_text(0, 25, text='ABCDEFGH', fill='blue')

#
# MainWindow
#
class MainWindow(tk.Frame):
    def __init__(self, master=None):
        self.parent = master
        tk.Frame.__init__(self, self.parent, bg='bisque', borderwidth=1, relief="sunken")
        self.__create_layout()

    def __create_layout(self):
        self.frame1 = tk.Frame(self, bg="yellow")
        self.frame2 = tk.Frame(self, bg="blue")
        self.frame3 = LeftMiddle(self)  # tk.Frame(self, bg="green")
        self.frame4 = tk.Frame(self, bg="brown")
        self.frame5 = tk.Frame(self, bg="pink")

        self.frame1.grid(row=0, column=0, rowspan=4, columnspan=8, sticky=(tk.N, tk.S, tk.W, tk.E))
        self.frame2.grid(row=0, column=8, rowspan=4, columnspan=2, sticky=(tk.N, tk.S, tk.W, tk.E))
        self.frame3.grid(row=4, column=0, rowspan=2, columnspan=5, sticky=(tk.N, tk.S, tk.W, tk.E))
        self.frame4.grid(row=4, column=5, rowspan=2, columnspan=5, sticky=(tk.N, tk.S, tk.W, tk.E))
        self.frame5.grid(row=5, column=0, rowspan=1, columnspan=10, sticky=(tk.N, tk.S, tk.W, tk.E))

        for r in range(6):
            self.rowconfigure(r, weight=1)
        for c in range(10):
            self.columnconfigure(c, weight=1)
#
#   MAIN
#
def main():
    root = tk.Tk()

    root.title("Frames")
    root.geometry("550x300+525+300")

    root.configure(background="#808080")
    root.option_add("*font", ("Courier New", 9, "normal"))

    window = MainWindow(master=root)
    window.pack(side="top", fill="both", expand=True)
    root.mainloop()

if __name__ == '__main__':
    main()

在此处输入图片说明

You have overlapping frames. 您有重叠的框架。 Both self.frame3 and self.frame4 are in row 4 with a rowspan of 2, meaning they occupy rows 4 and 5. self.frame5 is also in row 5. So, self.frame5 is obscuring the bottom half of self.frame3 , the frame that contains the canvas. 无论self.frame3self.frame4有4排为2的行跨度,这意味着他们占行4和5 self.frame5也是5。所以,行self.frame5是模糊的下半部分self.frame3 ,包含画布的框架。

I don't understand why you have so many rowspans, they seem completely unnecessary unless you have some specific reason why you want multiple rows and columns but only single frames that span these rows and columns. 我不明白为什么您会有这么多的行距,除非您有特定的原因为什么要多个行和列,而只需要跨越这些行和列的单个帧,否则它们似乎完全没有必要。 Looking at the screenshot I see the need for only three rows. 查看屏幕快照,我只需要三行。

The reason the text seems off is that by default the text is centered over the coordinate you give. 文本看起来不对的原因是,默认情况下,文本在您给定的坐标上居中。 You might want to look at the anchor option for the create_text method. 您可能需要查看create_text方法的anchor选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM