【问题标题】:Accessing Buttons in a frame Tkinter访问框架中的按钮 Tkinter
【发布时间】:2015-05-03 20:20:59
【问题描述】:

目前这是一个计划的一部分:

self.tabs = Notebook(root)
self.tabs.grid(column=0, row=0)   
self.frame1 = Frame()
self.frame2 = Frame()
self.frame3 = Frame()
self.frame4 = Frame()
self.frame5 = Frame()
self.frame6 = Frame()
self.framemain = Frame()
self.framemain.grid(column=1, row=0)

def buttonCreate(frameCount):
    buttonhatsforward = Button(self.frame1, image=self.forwardrender, command=select.buttonHatsForwardClicked).grid(column=3, row=0)
    button1 = Button(frameCount, text = "hello")
    button1.grid(column=0, row=1, sticky=W)
    button2 = Button(frameCount, text = "hello")
    button2.grid(column=1, row=1, sticky=W)
    button3 = Button(frameCount, text = "hello")
    button3.grid(column=2, row=1, sticky=W)
    button4 = Button(frameCount, text = "hello")
    button4.grid(column=3, row=1, sticky=W)
    button5 = Button(frameCount, text = "hello")
    button5.grid(column=4, row=1, sticky=W)

    button6 = Button(frameCount, text = "hello")
    button6.grid(column=0, row=2, sticky=W)
    button7 = Button(frameCount, text = "hello")
    button7.grid(column=1, row=2, sticky=W)
    button8 = Button(frameCount, text = "hello")
    button8.grid(column=2, row=2, sticky=W)
    button9 = Button(frameCount, text = "hello")
    button9.grid(column=3, row=2, sticky=W)
    button10 = Button(frameCount, text = "hello")
    button10.grid(column=4, row=2, sticky=W)

    button11 = Button(frameCount, text = "hello")
    button11.grid(column=0, row=3, sticky=W)
    button12 = Button(frameCount, text = "hello")
    button12.grid(column=1, row=3, sticky=W)
    button12 = Button(frameCount, text = "hello")
    button13.grid(column=2, row=3, sticky=W)
    button13 = Button(frameCount, text = "hello")
    button13.grid(column=3, row=3, sticky=W)
    button14 = Button(frameCount, text = "hello")
    button14.grid(column=4, row=3, sticky=W)

buttonCreate(self.frame1)
buttonCreate(self.frame2)
buttonCreate(self.frame3)
buttonCreate(self.frame4)
buttonCreate(self.frame5)
buttonCreate(self.frame6)


self.tab1 = self.tabs.add(self.frame1, text="tab1")
self.tab2 = self.tabs.add(self.frame2, text="tab2")
self.tab3 = self.tabs.add(self.frame3, text="tab3")
self.tab4 = self.tabs.add(self.frame4, text="tab4")
self.tab5 = self.tabs.add(self.frame5, text="tab5")
self.tab6 = self.tabs.add(self.frame6, text="tab6")

它的作用是制作一个框架,向其中添加按钮,然后将其添加到一些选项卡中。问题是,例如,当我执行self.button1.config(text="cookie") 时,它指的是最后一帧。我怎样才能做到这一点,以便我可以分别访问每个按钮?

【问题讨论】:

  • 没有足够的代码继续。 buttonCreate 是课程的一部分吗?它没有self 参数。
  • 请提供 MCVE:stackoverflow.com/help/mcve
  • 完成。 buttonCreate 是我用来向每一帧添加按钮的工具,如下所示buttonCreate(self.frame1) 将其添加到第一帧,buttonCreate(self.frame2) 将其添加到第二帧等。理想情况下,我希望能够控制每个按钮都带有 self.frame1.button1.config(text = "hello") 之类的东西。
  • 我很有信心你的例子既不完整也不最小......
  • 这是一个涉及函数作用域的基本问题。研究如何从函数返回对象并保存对这些对象的引用。

标签: python button tkinter


【解决方案1】:

@TigerhawkT3,感谢您让我跳出框框思考。我快速洗了个澡,然后我意识到我可以这样做:return(button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14),然后将我的 buttonCreate 函数修改为 a = buttonCreate(self.frame1),最后是 a[2].configure(text="cake", compound=LEFT)

虽然返回 14 个按钮不是一个好主意,但我会继续努力使其更纤薄、更整洁。很遗憾你没有留下答案,我会投票给你的。 :(

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 2016-10-20
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多