Python 画布和网格 Tkinter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20149483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:40:34  来源:igfitidea点击:

Python Canvas and Grid Tkinter

python

提问by user3022474

I want to have a canvas in a grid BUT I want to separate the input and labels from the canvas, how so? that I can place the canvas without interfering the inputs and label so it will look "cute". Right now if I place the canvas in lets say row 0,3 then there will be a huge space and if there is another widget around it will either leave a big white space or make the widget long. I would like to place the canvas in the right and the other widgets in the left without leaving a big space anywhere
Any help would be appreciated

我想在网格中有一个画布,但我想将输入和标签与画布分开,怎么办?我可以在不干扰输入和标签的情况下放置画布,因此它看起来很“可爱”。现在,如果我将画布放在第 0,3 行,那么将会有一个巨大的空间,如果周围有另一个小部件,它将留下一个很大的空白或使小部件变长。我想将画布放在右侧,将其他小部件放在左侧,而不会在
任何地方留下大空间任何帮助将不胜感激

    import tkinter as tk
    from tkinter import *
    class Gui():
        def __init__(self, root):
            self.root=root
            self.entry = tk.Entry(root)
            stvar=tk.StringVar()
            stvar.set("one")
            self.option=tk.OptionMenu(root, stvar, "one", "two", "three")

            self.canvas=tk.Canvas(root, width=300, height=200, background='white')
            self.canvas.grid(row=0,column=2)
            label1=Label(self.root, text="Figure").grid(row=0,column=0, sticky="nw")
            label2=Label(self.root, text="X").grid(row=1,column=0, sticky="w")
            label3=Label(self.root, text="Y").grid(row=2,column=0, sticky="w")
            self.option.grid(row=0,column=1,sticky="nwe")
            entry = Entry(self.root).grid(row = 1,column = 1,sticky = E+ W)
            entry1 = Entry(self.root).grid(row = 2,column = 1, sticky = E)
            Button1=Button(self.root,text="Draw").grid(row = 3,column = 1, sticky = "we")
            figure1=self.canvas.create_rectangle(80, 80, 120, 120, fill="blue")

            #Grid.columnconfigure(self.root,1,weight=1, size=200)
    if __name__== '__main__':
        root=tk.Tk()
        gui=Gui(root)
        root.mainloop()

采纳答案by Kevin

You could create a Frameand place all of your non-canvas widgets in that. Their placement in the frame's grid will ignore the positioning of widgets outside the frame.

您可以创建一个Frame并将所有非画布小部件放在其中。它们在框架网格中的放置将忽略框架外小部件的定位。

import tkinter as tk
from tkinter import *
class Gui():
    def __init__(self, root):
        self.root=root
        self.entry = tk.Entry(root)
        stvar=tk.StringVar()
        stvar.set("one")

        self.canvas=tk.Canvas(root, width=300, height=200, background='white')
        self.canvas.grid(row=0,column=1)

        frame = Frame(self.root)
        frame.grid(row=0,column=0, sticky="n")

        self.option=tk.OptionMenu(frame, stvar, "one", "two", "three")
        label1=Label(frame, text="Figure").grid(row=0,column=0, sticky="nw")
        label2=Label(frame, text="X").grid(row=1,column=0, sticky="w")
        label3=Label(frame, text="Y").grid(row=2,column=0, sticky="w")
        self.option.grid(row=0,column=1,sticky="nwe")
        entry = Entry(frame).grid(row = 1,column = 1,sticky = E+ W)
        entry1 = Entry(frame).grid(row = 2,column = 1, sticky = E)
        Button1=Button(frame,text="Draw").grid(row = 3,column = 1, sticky = "we")
        figure1=self.canvas.create_rectangle(80, 80, 120, 120, fill="blue")

        #Grid.columnconfigure(self.root,1,weight=1, size=200)
if __name__== '__main__':
    root=tk.Tk()
    gui=Gui(root)
    root.mainloop()

Result:

结果:

enter image description here

在此处输入图片说明