-2

I have created a script and it is meant to insert a variable into the insert field. So far, I have been able to create the while loop which prints the variable, so i know its working.

The frustrating part is that it doesn't bring up the window, it only prints the variable:

import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk

root = Tk()

a= 0
while a<1:

    print a


    class Application(Frame):
            """GUI to display results of 'equity get'"""

            def __init__(self, master):
                """initialise the Frame"""
                Frame.__init__(self,master)
                self.grid()
                self.create_widgets()

            def create_widgets(self):
                """Create button, text and entry Widget"""
                """what it is i.e. label"""


                self.data = Label(self, text ="Data")
                self.data1 = Entry(self, width = 10)


                self.data.grid(column = 1, row = 1)
                self.data1.grid(column = 2, row = 1)
                self.data1.grid(column = 3, row = 1)            
                self.data1.insert(0,a)

app = Application(root)
root.title("reload test")
root.geometry("700x300")
root.mainloop()

The version below opens the window but the loop only iterates once, when in reality, i want it to continue all day.

import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk

root = Tk()

a= 0
while a<1:

    print a


    class Application(Frame):
            """GUI to display results of 'equity get'"""

            def __init__(self, master):
                """initialise the Frame"""
                Frame.__init__(self,master)
                self.grid()
                self.create_widgets()

            def create_widgets(self):
                """Create button, text and entry Widget"""
                """what it is i.e. label"""


                self.data = Label(self, text ="Data")
                self.data1 = Entry(self, width = 10)


                self.data.grid(column = 1, row = 1)
                self.data1.grid(column = 2, row = 1)
                self.data1.grid(column = 3, row = 1)            
                self.data1.insert(0,a)

    app = Application(root)
    root.title("reload test")
    root.geometry("700x200")
    root.mainloop()

Thank you @guest for your refernce to using the afterfunction, however, I am a little unsure of where it needs to go.

@Wooble the variable a will be a webscrape and I want it to insert the live information into my insert field Therefore, it will run a loop and insert what it finds!

Perhaps I am placing the after function in the incorrect place!

import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk

root = Tk()

a= 0
while a<1:
    url = "https://uk.finance.yahoo.com/q?s=ngq14.nym&ql=1"
    request= urllib2.Request(url)
    handle = urllib2.urlopen(request)
    content = handle.read()
    splitted_page24 = content.split("<span id=\"yfs_l10_ngq14.nym\">", 1);
    splitted_page24 = splitted_page24[1].split("</span>", 1)
    print splitted_page24[0]


    class Application(Frame):
            """GUI to display results of 'equity get'"""

            def __init__(self, master):
                """initialise the Frame"""
                Frame.__init__(self,master)
                self.grid()
                self.create_widgets()

            def create_widgets(self):
                """Create button, text and entry Widget"""
                """what it is i.e. label"""


                self.data = Label(self, text ="Data")
                self.data1 = Entry(self, width = 10)


                self.data.grid(column = 1, row = 1)
                self.data1.grid(column = 2, row = 1)
                self.data1.grid(column = 3, row = 1)            
                self.data1.insert(0,splitted_page24[0])

    app = Application(root)
    root.title("reload test")
    root.geometry("700x300")
    root.mainloop()
    root.after(15)                    
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
zachary
  • 3
  • 6
  • It will launch your application again when the mainloop exits. You almost certainly don't want to define a class and run a GUI mainloop in an infinite loop. What are you actually trying to accomplish here? – Wooble Jul 24 '14 at 12:23
  • It's cause of root.mainloop() which is a loop itself. If you want to do some updates within this loop you can use `after` and a function which handles the update, see this post: http://stackoverflow.com/questions/2400262/code-a-timer-in-a-python-gui-in-tkinter – a_guest Jul 24 '14 at 12:23

1 Answers1

0

Perhaps the biggest problem in your code is that you're calling mainloop inside another loop. That makes no sense whatsoever. Tkinter is designed for you to call mainloop exactly once, usually as the last executable line in your program, or last executable line in your main function.

It also doesn't make sense to define a class in a loop. I'm afraid this code needs a complete refactoring.

Finally, the reason you never see the contents of the window is that you aren't requesting that they are made visible. You are creating an instance of Application, which is a subclass of Frame. However, you neglect to ever call pack, place or grid on that instance, so it stays invisible.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685