简体   繁体   English

Python TTK笔记本错误地显示了选定的选项卡

[英]Python ttk notebook showing selected tab wrongly

from tkinter import *
from tkinter import ttk

class MainGame(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent        
        self.initUI()

    def tab_change(self, event):
        tab_id = self.page.index('current')
        print(tab_id, self.page.tab(tab_id, 'text'))

    def initUI(self):
        global canvas
        self.parent.title('PythonPage')
        self.pack(fill = BOTH, expand = 1)
        self.page = ttk.Notebook(self, width = 646 ,height = 629)
        self.page1 = Frame(self)
        self.page2 = Frame(self)
        self.page.add(self.page1, text = 'Tab1')
        self.page.add(self.page2, text = 'Tab2')
        self.page.bind('<ButtonPress-1>', self.tab_change)
        self.page.pack(expand = 0, anchor = 'w', side = 'top')

root = Tk()
root.geometry('925x650')
main = MainGame(root)
root.mainloop()

tab_change can show their id and names, but not correctly. tab_change可以显示其ID和名称,但显示不正确。

When Tab1 is clicked, I clicked Tab2 but it still print 0 Tab1 , it needs one more click to print 1 Tab2 . 单击Tab1 ,我单击了Tab2但它仍然打印0 Tab1 ,还需0 Tab1单击一次以打印1 Tab2

Tab2 click to Tab1 is the same, it needs one more click to show the current selected tab. Tab2单击与Tab1相同,需要再次单击以显示当前选定的选项卡。

I want to find why the tabs need double click? 我想找到为什么选项卡需要双击? And how can I get selected tab correctly by a single click? 怎样单击才能正确选择选项卡?

Change: 更改:

self.page.bind('<ButtonPress-1>', self.tab_change)

To: 至:

self.page.bind('<ButtonRelease-1>', self.tab_change)

Because unless you have released the pressed button, the tab hasn't changed! 因为除非您释放按下的按钮,否则选项卡不会更改!

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM