tkinter小部件界面交互式按钮

问题描述 投票:0回答:1

我对交互式python编程很新,所以请耐心等待。我在Python 3.3中使用PyCharm。

我正在尝试构建以下内容:

我想生成一个带有两个文本输入字段和两个按钮的交互式窗口的函数:

- 第一个按钮(START)运行一个小的文本搜索功能(我已经编写并测试过),而第二个按钮(QUIT)将退出应用程序。

- 第一个文本输入字段需要搜索一个字符串(例如:“Hello Stack World”),而另一个文本输入字段需要在第一个输入字符串中搜索一个字符串(例如:“Stack”)。

计划是,一旦填写了两个文本字段,按“开始”按钮将启动文本搜索功能,而“退出”按钮将停止程序。

问题是,'QUIT'按钮按照应有的方式工作,但'START'按钮什么都不做。我认为它实际上将我的程序发送到无限循环中。

任何和所有的帮助真的很感激。我是界面/小部件编程的新手。

提前致谢!

这是我现在的代码:

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
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(...)
...
© www.soinside.com 2019 - 2024. All rights reserved.