Tom Anonymous
Tom Anonymous

Reputation: 173

tkinter widget interface interactive button

I am very new to interactive python programming so please bear with me. I am using PyCharm with Python 3.3.

I am attempting to build the following:

I want to generate an a function that pulls up interactive window with two text input fields and two buttons:

-The first button (START) runs a small text-search function (which I already wrote and tested), while the second button (QUIT) will quit the app.

-The first text input field takes a string to be searched (ex: "Hello Stack World"), while the other text input field takes a string to be searched within the first input string (ex: "Stack").

The plan is that once the two text fields are filled in, pressing the 'START' button will start the text-search function, while the'QUIT' button stops the program.

The problem is, the 'QUIT' button works the way it should, but the 'START' button does nothing. I think it actually sends my program into an infinite loop.

Any and all help is really appreciated it. I am a novice at interface/widget programming.

Thanks in advance!

Here is my code as I have it now:

import tkinter
from tkinter import *

class Application(Frame):

def text_scan(self):
    dataf = str(input()) '''string to be searched'''
    s = str(input())     ''' string to search for'''
    ''' ... I will leave out the rest of this function code for brevity''' 

def createWidgets(self):

    root.title("text scan")
    Label (text="Please enter your text:").pack(side=TOP,padx=10,pady=10)
    dataf = Entry(root, width=10).pack(side=TOP,padx=10,pady=10)

    Label (text="Please enter the text to find:").pack(side=TOP,padx=10,pady=10)
    s = Entry(root, width=10).pack(side=TOP,padx=10,pady=10)

    self.button = Button(root,text="START",command=self.text_scan)
    self.button.pack()

    self.QUIT = Button(self)
    self.QUIT["text"] = "QUIT"
    self.QUIT["fg"] = "red"
    self.QUIT["command"] = self.quit

    self.QUIT.pack({"side": "left"})

def __init__(self, master=None):
    Frame.__init__(self, master)
    self.filename = None
    self.pack()
    self.createWidgets()

root = Tk()
root.title("text scan")
root.quit()
app = Application(master=root)
app.mainloop()

Upvotes: 0

Views: 2357

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

You cannot mix a GUI with input. To get the values from the entry widgets you need to do s.get() and dataf.get(). However, before you do that you need to remove the call to pack when creating the widgets, and move it to a separate statement. The reason is that pack returns None, so at the moment dataf and s are None. You also need to save references to these widgets as class attributes.

def text_scan(...):
    dataf_value = self.dataf.get()
    ...
...
self.dataf = Entry(...)
self.dataf.pack(...)
...

Upvotes: 2

Related Questions