gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:29:24 26 4
gpt4 key购买 nike

我的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com