Python Tkinter - 类
python对我来说是新手,我面对这个小小的,很可能对你们大多数人来说真的很容易解决, 问题。Python Tkinter - 类
我想第一次使用一个类,所以我没有做这么多的功能,只选择一个类!
所以这里是我迄今writen:
from tkinter import *
import webbrowser
class web_open3:
A = "webbrowser.open(www.google.de")
def open(self):
self.A = webbrowser.open("www.google.de")
test = web_open3.open()
root = Tk()
b1 = Button(root, text="button", command=test)
b1.pack()
root.mainloop()
的错误,我得到:
Traceback (most recent call last): line 11, in test = web_open3.open() TypeError: open() missing 1 required positional argument: 'self'
问候斯莱克
在编程类,一类是一个对象。什么是对象?这是一个例子。为了使用你的对象,你首先必须创建它。你可以通过实例化它,web = web_open3()
。然后,您可以使用open()
函数。
现在,对象也可能是静态的。一个静态对象,是一个你没有实例化的对象。任何类,不管是否被实例化,都可能有静态变量和函数。让我们来看看你的代码:
# Classes should be named with CamelCase convention: 'WebOpen3'
class web_open3:
# This is a static variable. Variables should be named with lowercase letters
A = "webbrowser.open(www.google.de"
# This is an instance method
def open(self):
# You are accessing a static variable as an instance variable
self.A = webbrowser.open("www.google.de")
# Here, you try to use an instance method without first initializing your object. That raises an error, the one you gave in the description.
test = web_open3.open()
现在让我们来看一个静态的例子:
class WebOpen3:
a = "webbrowser.open(www.google.de"
@staticmethod
def open():
WebOpen3.a = webbrowser.open("www.google.de")
test = WebOpen3.open()
和实例例如:
class WebOpen3:
def __init__(self):
self.a = "webbrowser.open(www.google.de"
def open(self):
self.a = webbrowser.open("www.google.de")
web = WebOpen3()
test = web.open()
那里仍留有一个问题。当说: test = web.open()
或test = WebOpen3.open()
时,您试图将返回值从open()
绑定到test
,但该函数不返回任何内容。所以,你需要给它添加一个return语句。让我们用实例方法/函数为例:
def open(self):
self.a = webbrowser.open("www.google.de")
return self.a
或者,而不是返回一个值,只需调用函数直接:
WebOpen3.open()
或
web.open()
Note: functions belonging to instances, are also called methods.
Note:
self
refers to an instance of that class.Note:
def __init__(self)
, is an instance´s initializer. For your case, you call it by usingWebOpen3()
. You will later find more special functions defined asdef __func_name__()
.Note: For more on variables in a class, you should read this: Static class variables in Python
至于您的Tkinter窗口的情况下,要获得您的观点中的按钮:您可以使用此代码:
from tkinter import *
app = Tk()
button = Button(app, text='Open in browser')
button.bind("<Button-1>", web.open) # Using 'web.open', or 'WebOpen3.open', both without parenthesis, will send a reference to your function.
button.pack()
app.mainloop()
不要给没有需要自我参数,因为A是一类变量
class web_open3:
A = "webbrowser.open(www.google.de"
def open():
web_opens.A = webbrowser.open("www.google.de")
然后初始化这样
test = web_open3()
好吧,我try'd它没有自我参数,但然后我得到“类型错误:打开()取0位置参数,但1被赋予” – SLake
@SLake我编辑了答案,错误是有点奇怪 –
你需要发起类第一可变= web_open3()。 init是在创建类的实例时运行的魔术函数。这是为了展示如何开始在python中编写一个类。
from tkinter import *
import webbrowser
class web_open3:
def __init__(self):
self.A = "http://www.google.de"
def open(self):
webbrowser.open_new(self.A)
test = web_open3()
root = Tk()
b1 = Button(root, text="button", command=test.open)
b1.pack()
root.mainloop()
就是这样!你真正的mvp!谢谢! – SLake
非常感谢!它有点帮助我更多地了解课程,但即使如此,我仍然很想解决这个问题......你能给我一个完整的代码和按钮的例子吗?当我尝试它仍然打开谷歌时,我运行它和按钮只是一个可点击的东西没有:> – SLake
是的,我想要做2或3个按钮来使用相同的类,但不同的方法又名URL's.from类(我认为多数民众赞成在即试图做:D) – SLake
@SLake我已经更新了我的答案的底部与相关信息。你说你的程序打开谷歌。我之前没有使用过'webbrowser',但是可能你调用open()函数呢?我建议在YouTube上找一些关于Tkinter的好教程。他们对我很有帮助。 – Andreas