python - 如何避免 tkinter 中的 checkbutton 被覆盖

标签 python user-interface tkinter

我的checkbutton被按钮覆盖。如何给按钮赋予x轴y轴

我尝试使用 l.place() 而不是 l.pack(),但它没有返回任何内容。我应该使用什么来代替 l.pack() 来避免这种覆盖?

代码:

import tkinter as tk
import os
import tkinter.filedialog


class ScrolledFrame(tk.Frame):

    def __init__(self, parent, vertical=True, horizontal=False):
        super().__init__(parent)

        self._canvas = tk.Canvas(self)
        self._canvas.grid(row=0, column=0)

        self._vertical_bar = tk.Scrollbar(self, orient="vertical", 
command=self._canvas.yview)
        if vertical:
            self._vertical_bar.grid(row=0, column=1, sticky="ns")
        self._canvas.configure(yscrollcommand=self._vertical_bar.set)

        self._horizontal_bar = tk.Scrollbar(self, orient="horizontal", 
command=self._canvas.xview)
        if horizontal:
            self._horizontal_bar.grid(row=1, column=0, sticky="we")
        self._canvas.configure(xscrollcommand=self._horizontal_bar.set)

        self.frame = tk.Frame(self._canvas)
        self._canvas.create_window((0, 0), window=self.frame, anchor="nw")

        self.frame.bind("<Configure>", self.resize)

    def resize(self, event=None): 
        self._canvas.configure(scrollregion=self._canvas.bbox("all"))


#This is not in class    
def call():

    # add to scrolled frame

    data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
    variables = []

    # add widgets to scrolled frame - as parent you have to use `sf.frame` instead of `sf`
    for txt in data:
        var = tk.IntVar()
        variables.append(var)
        l = tk.Checkbutton(sf.frame, text=txt, variable=var)
        l.pack()



root = tk.Tk()

sf = ScrolledFrame(root)
sf.pack()


btn1 = tkinter.Button(root, text="Source Path", command = call)
btn1.place(x = 0,y = 0 )

ent1 = tkinter.Entry(root)
ent1.place(x = 100,y = 0,width = 200,height=25)

btn1 = tkinter.Button(root, text="destination")
btn1.place(x = 0,y = 40 )

ent1 = tkinter.Entry(root)
ent1.place(x = 100,y = 40,width = 200,height=25)

root.mainloop()

输出不正确:

enter image description here

预期输出:

checkbutton 应在Destination Button 下方启动。

最佳答案

正如评论中提到的,不要混合使用各种布局。
那么,最好在您创建的框架中包含输入框和按钮。

这是我的建议,如果您愿意,您可以更改为其他布局:

import tkinter as tk
import os
import tkinter.filedialog

class ScrolledFrame(tk.Frame):

    def __init__(self, parent, vertical=True, horizontal=False):
        super().__init__(parent)

        self._canvas = tk.Canvas(self)
        self._canvas.grid(row=0, column=0)

        self._vertical_bar = tk.Scrollbar(self, orient="vertical", command=self._canvas.yview)
        if vertical:
            self._vertical_bar.grid(row=0, column=1, sticky="ns")
        self._canvas.configure(yscrollcommand=self._vertical_bar.set)

        self._horizontal_bar = tk.Scrollbar(self, orient="horizontal", command=self._canvas.xview)
        if horizontal:
            self._horizontal_bar.grid(row=1, column=0, sticky="we")
        self._canvas.configure(xscrollcommand=self._horizontal_bar.set)

        self.frame = tk.Frame(self._canvas)
        self._canvas.create_window((0, 0), window=self.frame, anchor="nw")

        self.frame.bind("<Configure>", self.resize)

        btn1 = tkinter.Button(root, text="Source Path", command = call)
        btn1.pack()

        ent1 = tkinter.Entry(root)
        ent1.pack()

        btn1 = tkinter.Button(root, text="destination")
        btn1.pack()

        ent1 = tkinter.Entry(root)
        ent1.pack()
        self.pack()

    def resize(self, event=None): 
        self._canvas.configure(scrollregion=self._canvas.bbox("all"))


def call():
    # add components to scrolled frame    
    data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
    variables = []

    # add widgets to scrolled frame - as parent you have to use `sf.frame` instead of `sf`
    for txt in data:
        var = tk.IntVar()
        variables.append(var)
        l = tk.Checkbutton(sf.frame, text=txt, variable=var)
        l.pack()


root = tk.Tk()
sf = ScrolledFrame(root)
root.mainloop()

当您单击该按钮时,您的勾选框将会出现。

关于python - 如何避免 tkinter 中的 checkbutton 被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47410970/

相关文章:

user-interface - HiveMQ 中 Web UI 的 SSL/TLS

python - 如何用 wxPython、PyQt、PySide 或 Tkinter 解释 Python?

python - Tensorflow 的多个版本导致问题

python - 如何在 python 中返回 lambda 中拆分的第一项?

python - 如何在wxpython中的按钮文本中写一个 "&"

android - AsyncTask ProgressDialog 不会更新对话框消息

python - sqlalchemy 设置关系

qt - 如何使用Qt Designer允许用户在窗口中的元素上调整大小?

python - 无法附加到剪贴板

python - 在 Python 中将 tkinter ttk Entry 文本滚动到末尾