tkinter小部件界面交互式按钮 [英] tkinter widget interface interactive button
问题描述
我对交互式python编程很陌生,所以请耐心等待。我在Python 3.3中使用PyCharm。
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:
- 第一个按钮(START)运行一个小的文本搜索功能(我已经编写并测试过),而第二个按钮(START)运行按钮(QUIT)将退出应用程序。
-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.
- 第一个文本输入字段需要搜索一个字符串(例如:Hello Stack World),而另一个文本input字段在第一个输入字符串中搜索要搜索的字符串(例如:Stack)。
-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").
计划是填写两个文本字段后按'START'按钮将启动文本搜索功能,而'QUIT'按钮将停止程序。
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.
问题是,'QUIT'按钮的工作方式它应该,但'START'按钮什么都不做。我认为它实际上将我的程序发送到无限循环中。
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.
提前致谢!
这是我现在的代码:
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()
推荐答案
您不能将GUI与输入
混合使用。要从条目小部件中获取值,您需要执行 s.get()
和 dataf.get()
。但是,在执行此操作之前,您需要在创建窗口小部件时删除对 pack
的调用,并将其移至单独的语句。原因是 pack
返回无
,所以目前 dataf
和 s
是无
。您还需要将对这些小部件的引用保存为类属性。
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(...)
...
这篇关于tkinter小部件界面交互式按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!