Tkinter笔记本-窗口宽度的选项卡太多
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tkinter笔记本-窗口宽度的选项卡太多相关的知识,希望对你有一定的参考价值。
我的第一个tkinter(Python 3)笔记本应用程序有问题。
用于显示数据的画布仅需宽775像素,高480像素。这一切都很好,直到选项卡的数量使窗口变宽为止。所有数据都放在一侧,另一侧是一片空白。我试图使笔记本小部件可滚动,但无法使其正常工作。
任何建议都会受到欢迎。
#!/usr/bin/python
# Try to work with older version of Python
from __future__ import print_function
import sys
if sys.version_info.major < 3:
import Tkinter as tk
import Tkinter.ttk as ttk
else:
import tkinter as tk
import tkinter.ttk as ttk
#============================================================================
# MAIN CLASS
class Main(tk.Frame):
""" Main processing
"""
def __init__(self, root, *args, **kwargs):
tk.Frame.__init__(self, root, *args, **kwargs)
self.root = root
self.root_f = tk.Frame(self.root)
self.width = 700
self.height = 300
# Create a canvas and scroll bar so the notebook can be scrolled
self.nb_canvas = tk.Canvas(self.root_f, width=self.width, height=self.height)
self.nb_scrollbar = tk.Scrollbar(self.root_f, orient='horizontal')
# Configure the canvas and scrollbar to each other
self.nb_canvas.config(yscrollcommand=self.nb_scrollbar.set,
scrollregion=self.nb_canvas.bbox('all'))
self.nb_scrollbar.config(command=self.nb_canvas.xview)
# Create the frame for the canvas window, and place
self.nb_canvas_window = tk.Frame(self.nb_canvas, width=self.width, height=self.height)
self.nb_canvas.create_window(0, 0, window=self.nb_canvas_window)
# Put the whole notebook in the canvas window
self.nb = ttk.Notebook(self.nb_canvas_window)
self.root_f.grid()
self.nb_canvas.grid()
self.nb_canvas_window.grid()
self.nb.grid(row=0, column=0)
self.nb_scrollbar.grid(row=1, column=0, sticky='we')
self.nb.enable_traversal()
for count in range(20):
self.text = 'Lots of text for a wide Tab ' + str(count)
self.tab = tk.Frame(self.nb)
self.nb.add(self.tab, text=self.text)
# Create the canvas and scroll bar for the tab contents
self.tab_canvas = tk.Canvas(self.tab, width=self.width, height=self.height)
self.tab_scrollbar = tk.Scrollbar(self.tab, orient='vertical')
# Convigure the two together
self.tab_canvas.config(xscrollcommand=self.tab_scrollbar.set,
scrollregion=self.tab_canvas.bbox('all'))
self.tab_scrollbar.config(command=self.tab_canvas.yview)
# Create the frame for the canvas window
self.tab_canvas_window = tk.Frame(self.tab_canvas)
self.tab_canvas.create_window(0, 0, window=self.tab_canvas_window)
# Grid the content and scrollbar
self.tab_canvas.grid(row=1, column=0)
self.tab_canvas_window.grid()
self.tab_scrollbar.grid(row=1, column=1, sticky='ns')
# Put stuff in the tab
for count in range(20):
self.text = 'Line ' + str(count)
self.line = tk.Label(self.tab_canvas_window, text=self.text)
self.line.grid(row=count, column=0)
self.root.geometry('x++'.format(self.width, self.height, 100, 100))
return
# MAIN (MAIN) =======================================================
def main():
""" Run the app
"""
# # Create the screen instance and name it
root = tk.Tk()
# # This wll control the running of the app.
app = Main(root)
# # Run the mainloop() method of the screen object root.
root.mainloop()
root.quit()
# MAIN (STARTUP) ====================================================
# This next line runs the app as a standalone app
if __name__ == '__main__':
# Run the function name main()
main()
好的,我想我现在明白了。这些卡舌位于笔记本计算机内部,与笔记本计算机不可分离。这样,笔记本将始终与其中的框架一样宽。为了获得想要的效果,我需要将画布放入笔记本中,然后在画布上添加标签。那是不允许的。回到绘图板上!
如果选项卡的宽度为“恒定”,并且您知道有多少个窗口适合所需的(固定的?)大小,则可以通过隐藏不适合宽度的选项卡来创建“滚动选项卡”小部件。创建左右两个按钮,例如,向右隐藏一个按钮,并向左显示下一个隐藏的按钮。
[如果有办法弄清楚标签的宽度(标签中的字体大小,填充等?),则可以更“动态”地进行。
我建议从以下位置组合解决方案:Is there a way to add close buttons to tabs in tkinter.ttk.Notebook?(以便能够关闭标签页)和此处:https://github.com/muhammeteminturgut/ttkScrollableNotebook使用按钮而不是滚动条来处理宽度问题。要使其生效,需要进行两个更改:将“ notebookTab”变量加载为CustomNotebook,并通过在第一个答案中切换style.layout的最里面子元素的顺序,将关闭图标放在左侧。这将产生可滑动和关闭的自定义笔记本类型。
以上是关于Tkinter笔记本-窗口宽度的选项卡太多的主要内容,如果未能解决你的问题,请参考以下文章
tkinter笔记:scale 尺度 (莫烦python笔记)
tkinter 笔记:创建输入框并显示结果 (莫烦python笔记)