蟒蛇Tkinter的widget界面交互按钮 [英] python tkinter widget interface interactive button

查看:138
本文介绍了蟒蛇Tkinter的widget界面交互按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的交互式Python编程所以请大家多多包涵。我使用PyCharm与Python 3.3。

我试图建立以下内容:

我要生成的交互式窗口有两个文本输入字段和两个按钮拉起功能:

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

-The第一个文本输入字段需要被搜索的字符串(如:你好堆栈世界),而其他的文本输入字段需要被第一个输入字符串中搜索的字符串(如:堆栈)。

我们的计划是,一旦这两个文本字段填写,pressing的开始按钮将启动文本搜索功能,同时the'QUIT按钮停止程序。

问题是,在退出按钮的工作原理应该的方式,但START按钮,什么也不做。我认为它实际上将我的程序进入一个无限循环。

任何及所有帮助真的是AP preciated它。我在接口/部件编程新手。

在此先感谢!

下面是我的code,因为我有现在:

 导入Tkinter的
从Tkinter的进口*类应用(帧):高清text_scan(个体经营):
    dataf = STR(输入())'''要查找的字符串'''
    S = STR(输入())'''要搜索的字符串'''
    '''...我将离开这一职能code其余为简便起见'''高清createWidgets(个体经营):    root.title(文字扫描)
    标签(文字=请输入您的文字:)。包(侧= TOP,padx = 10,pady = 10)
    dataf =条目(根,宽度= 10).pack(边= TOP,padx = 10,pady = 10)    标签(文字=请输入要查找的文本)。包(侧= TOP,padx = 10,pady = 10)
    S =项(根,宽度= 10).pack(边= TOP,padx = 10,pady = 10)    self.button =按钮(根,文本=START,命令= self.text_scan)
    self.button.pack()    self.QUIT =按钮(个体经营)
    self.QUIT [文字] =退出
    self.QUIT [FG] =​​红
    self.QUIT [命令] = self.quit    self.QUIT.pack({方:左})高清__init __(自走,主=无):
    框架.__的init __(自走,主)
    self.filename =无
    self.pack()
    self.createWidgets()根= TK()
root.title(文字扫描)
root.quit()
应用程序=应用程序(由主根)
app.mainloop()


解决方案

您不能混用输入一个GUI 。为了得到值从入门部件你需要做的 s.get() dataf.get()。不过,在此之前,你需要删除创建小部件时调用包,并将其移动到一个单独的声明。其原因是,收益,所以此刻 dataf 取值。您还需要为这些小部件引用保存为类的属性。

  DEF text_scan(...):
    dataf_value = self.dataf.get()
    ...
...
self.dataf =输入(...)
self.dataf.pack(...)
...

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

解决方案

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的widget界面交互按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆