0

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()
4

1 回答 1

2

您不能将 GUI 与input. 要从条目小部件中获取值,您需要执行s.get()dataf.get(). 但是,在您这样做之前,您需要pack在创建小部件时删除对的调用,并将其移至单独的语句。原因是pack回报None,所以此刻datafsNone。您还需要将这些小部件的引用保存为类属性。

def text_scan(...):
    dataf_value = self.dataf.get()
    ...
...
self.dataf = Entry(...)
self.dataf.pack(...)
...
于 2013-10-13T19:55:32.317 回答