2016-08-30 38 views
0
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可以顯示其ID和名稱,但不正確。Python ttk筆記本錯誤地顯示選項卡

當點擊Tab1時,我點擊了Tab2,但仍然打印0 Tab1,需要再點擊一次才能打印1 Tab2

Tab2點擊到Tab1是一樣的,它需要再點擊一次才能顯示當前選中的標籤。

我想找到爲什麼標籤需要雙擊?我怎樣才能通過一次點擊正確選擇標籤?

回答

0

變化:

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

要:

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

因爲除非你已經釋放按下的按鈕,標籤並沒有改變!

enter image description here

+0

謝謝,我只是認爲他們是一樣的按鈕 – Montague27

相關問題