gpt4 book ai didi

python - 当tkinter中的输入框超过窗口高度时,如何添加滚动条?

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:35 25 4
gpt4 key购买 nike

首先,当输入的矩形数量小于 24 时,它可以正常工作。当我放置的矩形数量超过 24 时,输入框超出了窗口高度,我看不到它。当输入框的数量超过窗口高度时,我想在右侧使用滚动条。该文档暗示只有 List、Textbox、Canvas 和 Entry 小部件支持滚动条界面。那么,我该如何处理网格盒呢?

注意:该代码是可验证的。

from tkinter import *

temp_recta = []

def save_content(top):
for j in range(0, int(rectangles.get()) * 2):
temp_recta.append(rectangle_values[j].get())

root = Toplevel()

l1 = Label(root, text="Size of Sheet[Height]:")
l1.grid(row=0, column=0)

l2 = Label(root, text="Size of Sheet[Width]:")
l2.grid(row=1, column=0)

height = StringVar()
e1 = Entry(root, textvariable=height)
e1.grid(row=0, column=1)

width = StringVar()
e2 = Entry(root, textvariable=width)
e2.grid(row=1, column=1)

b5 = Button(root, text="Proceed", width=12, command=lambda: save_sheetsize(e1, e2, root))
b5.grid(row=2, column=0)
b6 = Button(root, text="Back", width=12, command=lambda: (root.destroy(), top.deiconify()))
b6.grid(row=2, column=1)

root.title("Sheet Size")

def save_sheetsize(e1, e2, root):
global x
global y
x = float(e2.get())
y = float(e1.get())
root.destroy()
window.destroy()

rectangle_values = []
x = 0
y = 0
k = 0

def open_window():
window.withdraw()
global k
top = Toplevel()
top.title("Rectangles")

for i in range(0, int(rectangles.get()) * 2):
if (i % 2) == 0:
l4 = Label(top, text="Size of rectangle:")
l4.grid(row=i, column=0)

rectangle_values.append(StringVar())
en = Entry(top, textvariable=rectangle_values[i])
en.grid(row=i, column=1)


b3 = Button(top, text="Save", width=12, command=lambda: (top.withdraw(), save_content(top)))
b3.grid(row=int(rectangles.get()) * 2 + 1, column=0)
b4 = Button(top, text="Back", width=12, command=lambda: (top.destroy(), window.deiconify()))
b4.grid(row=int(rectangles.get()) * 2 + 1, column=1)
k = int(rectangles.get())

window = Tk()

l3 = Label(window, text="Number of Rectangles:")
l3.grid(row=0, column=0)

rectangles = StringVar()
e3 = Entry(window, textvariable=rectangles)
e3.grid(row=0, column=1)

b1 = Button(window, text='Submit', width=12, command=open_window)
b1.grid(row=3, column=1)

window.title("Rectangle Configuration")
window.mainloop()

最佳答案

这是一个简单的类来执行此操作。添加您的文本小部件,当您的窗口充满文本时,它会给您一个滚动条。

import tkinter
class Scrollbar:
def __init__(self,text):
self.frame = text.master
self.text = text
self.text.configure(wrap='none')
self.for_x_view()
self.for_y_view()

def for_x_view(self):
# scroll Bar x For width
scroll_x=tkinter.Scrollbar(self.frame, orient='horizontal',command=self.text.xview)
scroll_x.config(command=self.text.xview)
self.text.configure(xscrollcommand=scroll_x.set)
scroll_x.pack(side='bottom', fill='x', anchor='w')
return

def for_y_view(self):
# Scroll Bar y For Height
scroll_y = tkinter.Scrollbar(self.frame)
scroll_y.config(command=self.text.yview)
self.text.configure(yscrollcommand=scroll_y.set)
scroll_y.pack(side='right', fill='y')
return


if __name__ == '__main__':
root = tkinter.Tk()
pad = tkinter.Text(root,wrap='none')
Scrollbar(pad)
pad.pack()
root.mainloop()

关于python - 当tkinter中的输入框超过窗口高度时,如何添加滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54878083/

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