MacOS Python2.7 ttk.Notebook多个tab中的Scrollbar响应异常

我在MacOS/Python 2.7下使用Tkinter Notebook创建了2个Tab,每个Tab中包含一个带着Scrollbar的Text控件,我的代码如下,在运行时出现个问题:
过程如下:

  1. 程序启动后,第一次用鼠标点选Tab 1,滑动Scrollbar可以移动
  2. 程序启动后,第一次用鼠标点选Tab 2,滑动Scrollbar可以移动
    3. 但是在点击Tab 2并操作滑块后,再返回来点击选择Tab 1,此时Tab 1中的滑块却没有响应,不能移动!

请教高手以下这样的操作错误在哪里?有什么解决方法?

# coding=utf-8
from Tkinter import (Tk, Text, Scrollbar)
from Tkconstants import (END, RIGHT, X, Y, BOTH, YES)
import ttk

def add_tab_with_text(master,title):
    '''向ttk.Notebook中添加一个tab,并在tab中添加一个带scrollbar的Text控件'''
    mytab = ttk.Frame(master)
    master.add(mytab, text=title)

    # 设置Scrollbar
    scrollbar_v = Scrollbar(mytab)
    scrollbar_v.pack(side=RIGHT, fill=Y)

    # 设置Text控件
    mytext = Text(mytab, width=40, height=40)
    mytext.config(yscrollcommand=scrollbar_v.set)
    mytext.pack(expand=YES,fill=BOTH)

    # Text控件中添加内容
    for i in range(1, 1000):
        mytext.insert(END, '%s line: %d.0\n' % (title,i))

    # Scrollbar动作bind
    scrollbar_v.config(command=mytext.yview)

main_win = Tk()
main_win.title('Scrollbar test')
main_win.geometry('800x800')

nb = ttk.Notebook(main_win)
add_tab_with_text(nb,'tab 1') # 创建Tab 1
add_tab_with_text(nb,'tab 2') # 创建Tab 2

nb.pack(fill='both',side='top')
main_win.mainloop()
Jason990420
最佳答案

Revised to following code and run under Python 3.7.4, and everything fine.

from tkinter import (Tk, Text, Scrollbar)
from tkinter import (END, RIGHT, X, Y, BOTH, YES)
from tkinter import ttk

建议不要使用Mac上原有的Python版本,最好是从Python.org下载适合的版本,因为其所包含的tkinter极有可能有一大堆问题。

2年前 评论
Auspark (楼主) 2年前
讨论数量: 1
Jason990420

Revised to following code and run under Python 3.7.4, and everything fine.

from tkinter import (Tk, Text, Scrollbar)
from tkinter import (END, RIGHT, X, Y, BOTH, YES)
from tkinter import ttk

建议不要使用Mac上原有的Python版本,最好是从Python.org下载适合的版本,因为其所包含的tkinter极有可能有一大堆问题。

2年前 评论
Auspark (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!