python - Run a function inside a tkinter -
i have created function reads strings list.
the function should displayed (or rather called) within frame created tkinter.
my thought create function method of frame class (or maybe method of label) , phone call method new instance. result was, content displayed in console.
my code far:
from tkinter import * root = tk() = 1 b = -6 c = -5 def calc_x(y): x = 0 if y == c: in range(1,len(data)): x = x + int(data[i][y]) t = x/(len(data)-1) print str(t)+"%" else: in range(1,len(data)): x = x + int(data[i][y]) print x class fram(frame): def __init__(self,tk,width,height): frame.__init__(self,tk) self.config(width=width) self.config(height=height) self.config(bd=1) self.config(relief="solid") square_frame = fram(root,500,40).grid(row=0,column=0,padx=3,pady=3) mainloop()
edit:
ok, give thanks much far. added "data" list souce url.
how can phone call function either of variables (self.a/b/c) ? when create instance of "fram" doesn't display string.
from tkinter import * urllib2 import * import csv root = tk() class fram(frame): def __init__(self,tk,width,height): frame.__init__(self,tk) self.config(width=width) self.config(height=height) self.config(bd=1) self.config(relief="solid") self.url = "http://data.nottinghamtravelwise.org.uk/parking.csv?nolocation=true?t=635509084580321642" self.webpage = urlopen(self.url) self.datareader = csv.reader(self.webpage.read().decode('utf-8').splitlines()) self.data = list(self.datareader) self.a = 1 self.b = -6 self.c = -5 self.value = self.calc_x(self.a) self.label = label(self, text=self.value) self.label.pack() def calc_x(self, y): x = 0 if y == self.c: in range(1,len(data)): x = x + int(data[i][y]) t = x/(len(data)-1) homecoming str(t)+'%' else: in range(1,len(data)): x = x + int(data[i][y]) homecoming str(x)
i think have 2 options, can either utilize label widget or text widget. label widget more useful short strings , phone call this:
self.label = label(self, text="text inserted") self.label.pack()
the text widget can accessed this:
self.text = text(self, height=30, width=30) self.text.pack() self.text.insert(end, "text inserted")
also sounds of should think moving function within of class , calling __init__
self.calcx(y)
. way class have access info need.
edit (try out):
from tkinter import * urllib2 import * import csv root = tk() class fram(frame): def __init__(self,tk,width,height): frame.__init__(self,tk) self.config(width=width) self.config(height=height) self.config(bd=1) self.config(relief="solid") self.url = "http://data.nottinghamtravelwise.org.uk/parking.csv?nolocation=true?t=635509084580321642" self.webpage = urlopen(self.url) self.datareader = csv.reader(self.webpage.read().decode('utf-8').splitlines()) self.data = list(self.datareader) self.a = 1 self.b = -6 self.c = -5 self.value = self.calc_x(self.a, self.data) self.label = label(self, text=self.value) self.label.pack() def calc_x(self, y, data): x = 0 if y == self.c: in range(1,len(data)): x = x + int(data[i][y]) t = x/(len(data)-1) homecoming str(t)+'%' else: in range(1,len(data)): x = x + int(data[i][y]) homecoming str(x) square_frame = fram(root,500,40).grid(row=0,column=0,padx=3,pady=3) mainloop()
python oop tkinter
No comments:
Post a Comment