我的checkbutton
用按钮覆盖。如何给按钮赋予x axis
和y axis
?
我尝试使用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()
错误的输出:
预期的输出:
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()
当您单击按钮时,您的复选框将出现。