我的第一个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()
答案 0 :(得分:0)
好的,所以我想我现在明白了。标签位于笔记本内部,与笔记本电脑不兼容。因此,笔记本电脑将始终与其中的帧一样宽。为了获得我想要的效果,我需要将画布放入笔记本中,然后在画布上添加选项卡。这是不允许的。所以回到绘图板!
答案 1 :(得分:0)
如果标签的宽度是“常量”,并且您知道有多少适合窗口的所需(固定?)大小,则可以通过隐藏不适合宽度的标签来创建“滚动标签”小部件。创建左右两个按钮,例如将一个按钮隐藏到右侧,并显示左侧的下一个隐藏按钮。
如果有办法找出标签的宽度(标签中的fontsize,填充等?),可以更加“动态”地完成。
答案 2 :(得分:0)
我建议从以下位置组合解决方案:Is there a way to add close buttons to tabs in tkinter.ttk.Notebook?(以便能够关闭标签页)和此处:https://github.com/muhammeteminturgut/ttkScrollableNotebook,以便使用按钮而不是滚动条来处理宽度问题。 要使其生效,需要进行两个更改:将“ notebookTab”变量加载为CustomNotebook,并通过在第一个答案中切换style.layout的最里面子元素的顺序,将关闭图标放在左侧。这将产生可滑动和关闭的自定义笔记本类型。