Python 2.7 Tkinter 类
Python 2.7 Tkinter classes
我正在尝试使用 tkinter 创建一个基础 class,它可以显示解析到它的任何变量的内容。
我的目标是有多个 classes 将变量传递给要显示的 class。到目前为止,我有以下代码,但我无法让它按预期工作。
任何帮助将不胜感激,谢谢
from Tkinter import *
class interface:
def __init__(self, root):
root.title("Testing tkinter and labels")
root.geometry("600x200")
def text(str1, str2):
Label(root, text = str1).pack(expand=1)
Label(root, text = str2).pack(expand=1)
app = Frame(root)
app.pack(side = 'bottom')
button1 = Button(app, text="Next")
button1.pack()
str1 = "hello"
str2 = "bye"
root = Tk()
interface(root)
interface.text(str1, str2)
root.mainloop()
我想你应该有这个:
def text(self, str1, str2): #<-- you forgot self
然后代替这个:
interface(root)
interface.text(str1, str2)
应该是:
int_obj = interface(root) # you need to create a variable to hold an instance of the interface you create. Otherwise, its immediately deleted.
int_obj.text(str1, str2)
我正在尝试使用 tkinter 创建一个基础 class,它可以显示解析到它的任何变量的内容。
我的目标是有多个 classes 将变量传递给要显示的 class。到目前为止,我有以下代码,但我无法让它按预期工作。
任何帮助将不胜感激,谢谢
from Tkinter import *
class interface:
def __init__(self, root):
root.title("Testing tkinter and labels")
root.geometry("600x200")
def text(str1, str2):
Label(root, text = str1).pack(expand=1)
Label(root, text = str2).pack(expand=1)
app = Frame(root)
app.pack(side = 'bottom')
button1 = Button(app, text="Next")
button1.pack()
str1 = "hello"
str2 = "bye"
root = Tk()
interface(root)
interface.text(str1, str2)
root.mainloop()
我想你应该有这个:
def text(self, str1, str2): #<-- you forgot self
然后代替这个:
interface(root)
interface.text(str1, str2)
应该是:
int_obj = interface(root) # you need to create a variable to hold an instance of the interface you create. Otherwise, its immediately deleted.
int_obj.text(str1, str2)