访问框架中的按钮

2024-05-13 01:45:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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")时,它引用最后一帧。我如何才能使它,使我可以访问每个按钮分别?你知道吗


Tags: textselfaddhellocolumnbuttonframegrid
1条回答
网友
1楼 · 发布于 2024-05-13 01:45:11

@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个按钮不是一个好主意,我会继续努力,使它更苗条和整洁。可惜你没有留下答案,我会投你一票的。:(

相关问题 更多 >