【发布时间】:2013-10-21 08:35:17
【问题描述】:
我对交互式 python 编程非常陌生,所以请多多包涵。我正在将 PyCharm 与 Python 3.3 一起使用。
我正在尝试构建以下内容:
我想生成一个带有两个文本输入字段和两个按钮的交互式窗口的函数:
-第一个按钮 (START) 运行一个小的文本搜索功能(我已经编写并测试过),而第二个按钮 (QUIT) 将退出应用程序。
-第一个文本输入字段采用要搜索的字符串(例如:“Hello Stack World”),而另一个文本输入字段采用要在第一个输入字符串中搜索的字符串(例如:“Stack”)。
计划是一旦两个文本字段都填写完毕,按“START”按钮将启动文本搜索功能,而“QUIT”按钮将停止程序。
问题是,“退出”按钮按应有的方式工作,但“开始”按钮没有任何作用。我认为它实际上将我的程序发送到一个无限循环中。
非常感谢任何和所有的帮助。我是界面/小部件编程的新手。
提前致谢!
这是我现在拥有的代码:
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()
【问题讨论】:
标签: python button interface widget tkinter