Python Forum
Class Struggle: Connecting a Method to an Outside Variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Struggle: Connecting a Method to an Outside Variable
#3
(Jan-21-2018, 09:07 PM)Gribouillis Wrote: You want the bind() method:
myVar.bind('Button-1', myMethod)
I've tried to insert your suggestion in a couple of different ways now, but all attempts result in either:

NameError: name 'myVar' is not defined
or:
NameError: name 'label_1' is not defined

I did a bit of reading on the subject, and it turns out that 'Button-1' is a fixed expression within the realm of the bind method, hence it wont bind to a label or a text field or any object which is not a button in tkinter. - Right? Shifty

Thank you all the same; now I know there's a method called bind. Any type of progress is good progress :)

For clarity, here's version 1 (without using classes). This one works just fine.

from tkinter import *
import requests
from bs4 import BeautifulSoup
import re

#-------------------------------#
# - request module - Forecast - #
#-------------------------------#

data1 = requests.get('https://www.dmi.dk/vejr/til-lands/byvejr/by/vis/DK/1000/K%C3%B8benhavn,%20Danmark/')
soup = BeautifulSoup(data1.text, 'html.parser')
#print(soup)
data2 = soup.find('div',{'id':'NJ'})
#print(data2)
data3 = data2.find_all('p')
#print(data3)
data4 = data3[2]

#-------------------------------#
#       - request modules -     #
#-------------------------------#

blabla = requests.get('https://www.dmi.dk/vejr/til-lands/regionaludsigten/kbhnsjaelland/')
toast = BeautifulSoup(blabla.text, 'html.parser')
#print(toast)
bleble = toast.find('div', {'id':'c5886'})
#print(bleble)
blibli = bleble.find_all('td')
#print(blibli)

#temp
bloblo = blibli[2]
#print(bloblo.string[0:6])

#fugt
blublu = blibli[3]
#print(blublu.string[0:5])

#vind
blybly = blibli[5]
#print(blybly.string[0:5])

#solopgang
blaxblax = toast.find('div', {'id':'c3065'})
#print(blaxblax)
blexblex = blaxblax.find_all('p')
#print(blexblex)

blixblix = str(blexblex[2])
#print(blixblix)
#print(type(blixblix))

bloxblox = re.search('solnedgang: ', blixblix)
#print(str(bloxblox))
start = bloxblox.end()
end = start + 10
#print(blixblix[start:end])

#-------------------------------#
#       - tkinter window -      #
#-------------------------------#

root = Tk()
root.title("Vejrstation 1.0")
root.geometry("350x360")
root.resizable(False, False)
#root.resizable(False, False)

#-------------------------------#
#        - button funcs -       #
#-------------------------------#

def butt1_func():
    print("it's alive.")

def butt2_func():
    print("This one is also alive.")

def butt3_func():
    quit()

#-------------------------------#
# - labels, buttons and text -  #
#-------------------------------#

"""smølfe om til classes, måske?"""

label_1 = Label(root, text="Temp.")
label_1.grid(row=0, column=0)
label_1 = Label(root, text="Fugt.")
label_1.grid(row=0, column=1)
label_1 = Label(root, text="Vind")
label_1.grid(row=0, column=2)

textbox_1 = Text(root, height=1,width=10)
textbox_1.grid(row=1, column=0)
textbox_1.insert(END, bloblo.string[0:6])

textbox_1 = Text(root, height=1,width=10)
textbox_1.grid(row=1, column=1)
textbox_1.insert(END, blublu.string[0:5])

textbox_1 = Text(root, height=1,width=10)
textbox_1.grid(row=1, column=2)
textbox_1.insert(END, blybly.string[0:6])

#spacer
spacer = Label(root)
spacer.grid(row=2)

label_3 = Label(root, text="Solopgang/solnedgang")
label_3.grid(row=3, column=0, columnspan=1)

textbox_3 = Text(root, height=1, width=10)
textbox_3.grid(row=4, column=0, columnspan=1)
textbox_3.insert(END, blixblix[start:end])

#spacer
spacer = Label(root)
spacer.grid(row=5)

label_2 = Label(root, text="DMI Vejrudsigt")
label_2.grid(row=6, column=0, columnspan=3)

Scroller = Scrollbar(root)
textbox_2 = Text(root, height=10, width=49, wrap=WORD)
Scroller.grid(row=7, column=0)
textbox_2.grid(row=7, column=0, columnspan=3)
Scroller.config(command=textbox_2.yview)
textbox_2.config(yscrollcommand=Scroller.set)
textbox_2.insert(END, data4.string)

#spacer
spacer = Label(root)
spacer.grid(row=8)

label_2 = Label(root, text="Nogle forskellige knapper")
label_2.grid(row=9, column=0, columnspan=3)

button_1 = Button(root, text="SEND!", command=butt1_func)
button_1.grid(row=10, column=0, sticky="NWNESWSE")
button_2 = Button(root, text="ASSIGN!", command=butt2_func)
button_2.grid(row=10, column=1, sticky="NWNESWSE")
button_3 = Button(root, text="ABORT!", command=butt3_func)
button_3.grid(row=10, column=2, sticky="NWNESWSE")

#-------------------------------#
#       - run that sucka! -     #
#-------------------------------#

root.mainloop()
And here's version 2 in which I would like to create classes for the elements involved (doesn't work; computer says nooooo!):

from tkinter import *
import requests
from bs4 import BeautifulSoup
import re

#-------------------------------#
# - request module - Forecast - #
#-------------------------------#

data1 = requests.get('https://www.dmi.dk/vejr/til-lands/byvejr/by/vis/DK/1000/K%C3%B8benhavn,%20Danmark/')
soup = BeautifulSoup(data1.text, 'html.parser')
#print(soup)
data2 = soup.find('div',{'id':'NJ'})
#print(data2)
data3 = data2.find_all('p')
#print(data3)
data4 = data3[2]

#-------------------------------#
#   - other request modules -   #
#-------------------------------#

blabla = requests.get('https://www.dmi.dk/vejr/til-lands/regionaludsigten/kbhnsjaelland/')
toast = BeautifulSoup(blabla.text, 'html.parser')
#print(toast)
bleble = toast.find('div', {'id':'c5886'})
#print(bleble)
blibli = bleble.find_all('td')
#print(blibli)

#temp
bloblo = blibli[2]
#print(bloblo.string[0:6])

#fugt
blublu = blibli[3]
#print(blublu.string[0:5])

#vind
blybly = blibli[5]
#print(blybly.string[0:5])

#solopgang
blaxblax = toast.find('div', {'id':'c3065'})
#print(blaxblax)
blexblex = blaxblax.find_all('p')
#print(blexblex)

blixblix = str(blexblex[2])
#print(blixblix)
#print(type(blixblix))

bloxblox = re.search('solnedgang: ', blixblix)
#print(str(bloxblox))
start = bloxblox.end()
end = start + 10
#print(blixblix[start:end])

#-------------------------------#
#       - tkinter window -      #
#-------------------------------#

root = Tk()
root.title("Vejrstation 1.0")
root.geometry("350x350")
#root.resizable(True, True)
root.resizable(False, False)

#-------------------------------#
#        - button funcs -       #
#-------------------------------#

def butt1_func():
    print("it's alive.")

def butt2_func():
    print("This one is also alive.")

def butt3_func():
    quit()

#-------------------------------#
#         - classes -           #
#-------------------------------#

class lbl(object):
    def __init__(self, pos, txt, row, col, colspan):
        self.position = pos
        self.text = txt
        self.row = row
        self.column = col
        self.columnspan = colspan

        myVar = Label(self.position, self.text)
        myVar.grid(self.row, self.column, self.columnspan)

class spcr(object):
    def __init__(self, pos, row, col, colspan):
        self.position = pos
        self.row = row
        self.column = col
        self.columnspan = colspan

        myVar = Label(self.position)
        myVar.grid(self.row, self.column, self.columnspan)

class txtbox(object):
    def __init(self, pos, height, width, wrap, txt, row, col, colspan):
        self.position = pos
        self.height = height
        self.width = width
        self.text = txt
        self.row = row
        self.column = col
        self.columnspan = colspan
        self.wrap = wrap

        myVar = Text(self.position, self.height, self.width, self.wrap)
        myVar.grid(self.row, self.column, self.columnspan)
        myVar.insert(END, self.text)

class btn(object):
    def __init__(self, pos, txt, cmd, row, col, colspan, sticky):
        self.position = pos
        self.text = txt
        self.command = cmd
        self.row = row
        self.column = col
        self.columnspan = colspan
        self.sticky = sticky

        myVar = Button(self.position, self.text, self.cmd)
        myVar.grid(self.row, self.column, self.columnspan, self.sticky)

label_1 = lbl(root, "temp.", 0, 0, 1)
label_2 = lbl(root, "fugt", 0, 1, 1)
label_3 = lbl(root, "vind", 0, 2, 1)

textbox_1 = txtbox(root, 1, 10, None, 1, 0, 1)
textbox_2 = txtbox(root, 1, 10, None, 1, 1, 1)
textbox_3 = txtbox(root, 1, 10, None, 1, 2, 1)

spacer_1 = spcr(root, 2, 0, 3)
label_4 = lbl(root, "solopgang/solnedgang", 3, 0, 1)
textbox_4 = txtbox(root, 1, 10, None, 4, 0, 1)

spacer_2 = spcr(root, 5, 0, 3)
label_5 = lbl(root, "DMI vejrudsigt", 6, 0, 3)
textbox_5 = txtbox(root, 10, 49, None, 7, 0, 3)

spacer_3 = spcr(root, 8, 0, 3)
label_6 = lbl(root, "Nogle forskellige knapper", 9, 0, 3)

button_1 = btn(root, "TRANSMIT!", butt1_func, 10, 0, 1, "NWNESWSE")
button_2 = btn(root, "UPDATE!", butt2_func, 10, 1, 1, "NWNESWSE")
button_3 = btn(root, "ABORT!", butt3_func, 10, 2, 1, "NWNESWSE")


#-------------------------------#
#       - run that sucka! -     #
#-------------------------------#

root.mainloop()
Reply


Messages In This Thread
RE: Class Struggle: Connecting a Method to an Outside Variable - by vulpesVelox - Jan-22-2018, 11:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium big struggle Troop 2 1,710 Apr-25-2020, 10:47 AM
Last Post: Troop

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020