-2

python is new to me and I'm facing this little, probably for most of you really easy to solve, problem.

I am trying for the first time to use a class so I dont have to make so many functions and just pick one out of the class!!

so here is what I have writen so far:

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()

The Error I get :

Traceback (most recent call last): line 11, in test = web_open3.open() TypeError: open() missing 1 required positional argument: 'self'

greetings Slake

SLake
  • 24
  • 5

2 Answers2

2

You need to initiate a class first variable = web_open3(). The init is a magic function that is ran when you create an instance of the class. This is to show how to begin writing a class in 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()
Gumboy
  • 467
  • 4
  • 8
0

In programming, a class is an object. What is an object? It's an instance. In order to use your object, you first have to create it. You do that by instantiating it, web = web_open3(). Then, you can use the open() function.

Now, objects may also be static. A static object, is an object that you don't instantiate. Any class, independent of being instantiated or not, may have static variables and functions. Let's take a look at your code:

# 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()

Let's now look at a static example:

class WebOpen3:
    a = "webbrowser.open(www.google.de"

    @staticmethod
    def open():
        WebOpen3.a = webbrowser.open("www.google.de")

test = WebOpen3.open()

and an instance example:

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()

There is still one problem left. When saying: test = web.open(), or test = WebOpen3.open(), you're trying to bind the returning value from open() to test, however that function doesn't return anything. So, you need to add a return statement to it. Let's use the instance method/function as an example:

def open(self):
    self.a = webbrowser.open("www.google.de")
    return self.a

or, instead of returning a value, just call the function straight-forward:

WebOpen3.open()

or

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 using WebOpen3(). You will later find more special functions defined as def __func_name__().

Note: For more on variables in a class, you should read this: Static class variables in Python

As for the case of your Tkinter window, to get a button in your view: you can use this code:

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()
  • Thank you very much! It kinda help'd me to undertand more about classes but im still kinda to much noob to solve the problem... could you send me and example of the full code working on a button? When i try it still opens google when I run it and the button is just a clickable nothing :> – SLake Oct 29 '17 at 02:30
  • Yes exactly, I want to make 2 or 3 buttons to use the same class but diffrent methods aka URL's.from the class ( I think thats what im trying to do :D ) – SLake Oct 29 '17 at 02:36
  • @SLake I've updated the bottom of my answer with the relevant information. You said your program opens Google. I haven't used `webbrowser` before, but perhaps your call to the `open()` function does this? I'd suggest finding some good tutorials about Tkinter on youtube. They were quite helpful to me. – Andreas is moving to Codidact Oct 29 '17 at 02:46
  • 1
    I add code below that works, the button opens in whatever browser is default in you system. If you have any questions, type away. – Gumboy Oct 29 '17 at 02:53
  • 1
    Your explanation is superb. – Gumboy Oct 29 '17 at 03:02
  • Yesterday, I had an upvote on this answer. (Thanks for that, btw) When I came on today, that upvote was either removed, or someone down-voted the answer. Could you either tell me why it isn't good enough, perhaps editing it? If there's something wrong in it, I'd rather want to know what that is. – Andreas is moving to Codidact Oct 29 '17 at 09:05