【问题标题】:Python: Focus on ttk.Notebook tabsPython:专注于 ttk.Notebook 选项卡
【发布时间】:2015-01-22 11:53:48
【问题描述】:

我还没有弄清楚如何将焦点设置在 ttk.Notebook 的特定选项卡上。 focus_set 不起作用。有没有可能?

提前致谢

【问题讨论】:

    标签: python tkinter focus ttk


    【解决方案1】:

    我遇到了同样的问题。我发现笔记本的“选择”方法 (ttk.Notebook.select(someTabFrame)) 解决了这个问题:

    import ttk, Tkinter
    
    mainWindow = Tkinter.Tk()
    
    mainFrame = Tkinter.Frame(mainWindow, name = 'main-frame')
    mainFrame.pack(fill = Tkinter.BOTH) # fill both sides of the parent
    
    nb = ttk.Notebook(mainFrame, name = 'nb')
    nb.pack(fill = Tkinter.BOTH, padx=2, pady=3) # fill "master" but pad sides
    
    tab1Frame = Tkinter.Frame(nb, name = 'tab1')
    Tkinter.Label(tab1Frame, text = 'this is tab 1').pack(side = Tkinter.LEFT)
    nb.add(tab1Frame, text = 'tab 1')
    
    tab2Frame = Tkinter.Frame(nb, name = 'tab2')
    Tkinter.Label(tab2Frame, text = 'this is tab 2').pack(side = Tkinter.LEFT)
    nb.add(tab2Frame, text = 'tab 2')
    
    nb.select(tab2Frame) # <-- here's what you're looking for
    
    mainWindow.mainloop()
    

    ttk.Notebook 的 python 文档: https://docs.python.org/2/library/ttk.html#ttk.Notebook

    我还使用这篇博文作为我的代码模型: http://poquitopicante.blogspot.com/2013/06/blog-post.html

    【讨论】:

      【解决方案2】:

      此代码基于 wordsforthewise 对此问题的回答。在这里您可以找到使用 select 作为 get 和 set 函数的示例,它通过在 2 个选项卡之间切换的按钮显示。

      小改进:

      import ttk, Tkinter
      from pango import Weight
      from Tkinter import Button
      
      
      tab2Frame = None
      tab1Frame = None
      
      def switchTab():
          if nb.select()[-1] == "1":
              nb.select(tab2Frame)
          elif nb.select()[-1] == "2":
              nb.select(tab1Frame)
      
      mainWindow = Tkinter.Tk()
      
      mainWindow.geometry("%dx%d+0+0" % (200, 200))
      mainFrame = Tkinter.Frame(mainWindow, name = 'main-frame')
      mainFrame.pack(fill = Tkinter.BOTH) # fill both sides of the parent
      
      button = Button(mainWindow, text = "Switch", command = switchTab)
      button.configure(width = 15, activebackground = "#6f6Fff")
      button.pack()
      
      nb = ttk.Notebook(mainFrame, name = 'nb')
      nb.pack(fill = Tkinter.BOTH, padx=2, pady=3) # fill "master" but pad sides
      
      tab1Frame = Tkinter.Frame(nb, name = 'tab1')
      Tkinter.Label(tab1Frame, text = 'this is tab 1').pack(side = Tkinter.LEFT)
      nb.add(tab1Frame, text = 'tab 1')
      
      tab2Frame = Tkinter.Frame(nb, name = 'tab2')
      Tkinter.Label(tab2Frame, text = 'this is tab 2').pack(side = Tkinter.LEFT)
      nb.add(tab2Frame, text = 'tab 2')
      
      mainWindow.mainloop()
      

      【讨论】:

      • 你写了“小改进”。对什么的小改进?你能解释一下改进之处,这样人们就不必逐行比较你的解决方案和它的改进之处了吗?
      • 是的,如果这是一个小小的改进,那么编辑我的帖子可能会更好
      • @Bryan Oakley,我没有看到你的评论。对不起各位,我现在想不起来了。我会尝试稍后再回来。
      • 我知道这是一篇旧帖子,但nb.select()[-1] 对我来说所有标签都返回e。只是想我会指出这一点,因为它似乎没有返回索引或数字。
      猜你喜欢
      • 2018-04-29
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2015-10-19
      相关资源
      最近更新 更多