我一直试图返回函数printLabel
来打印“Hello”
世界!”,但我不太确定如何进一步发展:
我想使用lambda
,以便在单击按钮时在标签中打印我的附加字符串,但这会在没有单击按钮的情况下显示。我的
代码如下:
from tkinter import *
class Example(Frame):
def printLabel(self):
self.hello = []
self.hello.append('Hello\n')
self.hello.append('World!')
print(self.hello)
return(self.hello)
def __init__(self, root):
Frame.__init__(self, root)
self.buttonA()
self.viewingPanel()
def buttonA(self):
self.firstPage = Button(self, text="Print Text", bd=1, anchor=CENTER, height = 13, width = 13, command=lambda: self.printLabel())
self.firstPage.place(x=0, y=0)
def viewingPanel(self):
self.panelA = Label(self, bg='white', width=65, height=13, padx=3, pady=3, anchor=CENTER, text="{}".format(self.printLabel()))
self.panelA.place(x=100, y=0)
def main():
root = Tk()
root.title("Tk")
root.geometry('565x205')
app = Example(root)
app.pack(expand=True, fill=BOTH)
root.mainloop()
if __name__ == '__main__':
main()
我对你的代码做了一些修改,它应该按照你想要的方式工作:
相关问题 更多 >
编程相关推荐