Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Tkinter frame inside frame limitation or user error?
    primarykey
    data
    text
    <p>I've been building an app to track stock prices. The user should see a window with an entry widget and a button that creates a new frame with a label and a button. The label is the stock price and symbol, the button is a delete button, and should hide that frame if clicked.</p> <p>I've re-written this program 4 times now, and it's been a great learning experience, but what I've learned is that I can't have the "mini-frames" being called from methods part of the main GUI class - this funks up the delete buttons, and updates the value behind <code>frame.pack_forget()</code> so it only deletes the last item ever. </p> <p>I've moved my mini-frame widgets down into the class for the actual stock values. I've packed them (what I assume to be correct) but they don't show up. They also don't error out, which isn't very helpful. Here's my code, although I've omitted a lot of the functional parts to show what is happening with my frames. Keep in mind I need to keep it so that I can call my updater (<code>self.update_stock_value</code>) with a <code>.after</code> method against <code>myapp.myContainer</code>.</p> <p>Is there a better way to do this?? Thanks in advance, my head hurts.</p> <pre><code>import re import time import urllib from Tkinter import * import threading from thread import * runningThreads = 0 # each object will be added to the gui parent frame class MyApp(object): def __init__(self, parent): self.myParent = parent self.myContainer = Canvas(parent) self.myContainer.pack() self.create_widgets() # METHOD initiates basic GUI widgets def create_widgets(self): root.title("Stocker") self.widgetFrame = Frame(self.myContainer) self.widgetFrame.pack() self.input = Entry(self.widgetFrame) self.input.focus_set() self.input.pack() self.submitButton = Button(self.widgetFrame, command = self.onButtonClick) self.submitButton.configure(text = "Add new stock") self.submitButton.pack(fill = "x") # METHOD called by each stock object # returns the "symbol" in the entry widget # clears the entry widget def get_input_value(self): var = self.input.get() self.input.delete(0, END) return var # METHOD called when button is clicked # starts new thread with instance of "Stock" class def onButtonClick(self): global runningThreads # shhhhhh im sorry just let it happen runningThreads += 1 # count the threads open threading.Thread(target = self.init_stock,).start() # force a tuple if runningThreads == 1: print runningThreads, "thread alive" else: print runningThreads, "threads alive" def init_stock(self): new = Stock() class Stock(object): def __init__(self): # variable for the stock symbol symb = self.stock_symbol() # lets make a GUI self.frame = Frame(myapp.myContainer) self.frame.pack # give the frame a label to update self.testLabel = Label(self.frame) self.testLabel.configure(text = self.update_stock_label(symb)) self.testLabel.pack(side = LEFT) # create delete button to kill entire thread self.killButton = Button(self.frame, command = self.kill_thread) self.killButton.configure(text = "Delete") self.killButton.pack(side = RIGHT) # create stock label # call updater def kill_thread(self): global runningThreads runningThreads -= 1 self.stockFrame.pack_forget() # hide the frame self.thread.exit() # kill the thread def update_stock_label(self, symb): self.testLabel.configure(text = str(symb) + str(get_quote(symb))) myapp.myContainer.after(10000, self.update_stock_label(symb)) def stock_symbol(self): symb = myapp.get_input_value() print symb # The most important part! def get_quote(symbol): try: # go to google base_url = "http://finance.google.com/finance?q=" # read the source code content = urllib.urlopen(base_url + str(symbol)).read() # set regex target target = re.search('id="ref_\d*_l".*?&gt;(.*?)&lt;', content) # if found, return. if target: print "found target" quote = target.group(1) print quote else: quote = "Not Found: " return quote # handling if no network connection except IOError: print "no network detected" root = Tk() root.geometry("280x200") myapp = MyApp(root) root.mainloop() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload