python print命令在同一个打开的窗口中不在提示符中

时间:2015-04-11 16:13:37

标签: python user-interface tkinter window

好的,所以我正在尝试使用python,并遇到了这个小应用程序的问题,我试图将它放在一起。我试图在运行时打开一个窗口并要求输入关键字,然后将该输入与文件中的任何一行匹配。我输入输入并按下显示,但结果不显示或显示在提示中。我需要在打开的窗口中显示结果。可能在按下显示按钮时创建的新行和列上。所以结果只显示在窗口中,我在基于ubuntu的系统上。这是我的应用.py文件

#!/usr/bin/python
# -*- coding: utf-8 -*-

# sets the name settings
from Tkinter import *
class App(Frame):
def __init__(self, master=None):
    Frame.__init__(self, master)
    self.pack()

# runs the script to get full 
# list of programs for matching
#subprocess.call(["listcmnd.sh"])

# create the application
myapp = App()

# sets the e1 variable
e1 = Entry(myapp)

# checks keyword agianst 
# programs in the sam.txt file
import re

keyword = open("results/program_files/sam.txt", "r")

for line in keyword:
if re.match("str(text)", line):
    print line,

# prints results of keyword entry match
def show_entry_fields():
print("Program Name: %s" % (e1.get()))

# gives the output of findings
from Tkinter import *

# asks for search critiria
text = Label(myapp, text="Keyword: ").grid(row=0)

# displays the buttons for getting 
# results and closing program
Button(myapp, text='Quit', command=myapp.quit).grid(row=3, column=0,     sticky=W, pady=4)
Button(myapp, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

# prints results of keyword entry match
def show_entry_fields():
print("Program Name: %s" % (e1.get()))

# displays the text entry field
e1.grid(row=0, column=1)

#
# here are method calls to the window manager class
#
myapp.master.title("Program Search Tool")
myapp.master.maxsize(1000, 400)

# start the program
myapp.mainloop()

好吧所以我对主脚本进行了一些修改,但仍将结果输出到终端,而不是打开窗口

以下是新的代码更改

  #!/usr/bin/python
  # -*- coding: utf-8 -*-

  # sets the name settings
  from Tkinter import *
  class App(Frame):
      def __init__(self, master=None):
         Frame.__init__(self, master)
          self.pack()

  # runs the script to get full 
  # list of programs for matching
  #subprocess.call(["listcmnd.sh"])

  # create the application
  myapp = App()

  # sets the e1 variable
  e1 = Entry(myapp)

  # checks keyword agianst 
  # programs in the sam.txt file
  import re

  keyword = open("results/program_files/sam.txt", "r")

  for line in keyword:
    if re.match("get()(text)", line):
          print line,

  # prints results of keyword entry match
  def show_entry_fields():
    print("Program Name: %s" % (e1.get()))

  # asks for search critiria
  # displays the text entry field
  e1.grid(row=0, column=1)
  text = Label(myapp, text="Program Name: ")
  text.grid(row=3, column=0)

  # displays the buttons for getting 
  # results and closing program
  Button(myapp, text='Quit', command=myapp.quit).grid(row=4, column=0, sticky=W, pady=4)
  Button(myapp, text='Show', command=show_entry_fields).grid(row=2, column=0, sticky=W, pady=4)

  #
  # here are method calls to the window manager class
  #
  myapp.master.title("Program Search Tool")
  myapp.master.maxsize(1000, 1000)
  myapp.master.minsize(50, 50)

  # start the program
  myapp.mainloop()

1 个答案:

答案 0 :(得分:0)

你应该做的是在功能

中创建一个标签(或者可以在之前创建并更新它)
def show_entry_fields():

补充说明:

你多次导入Tkinter

你还定义了def show_entry_fields():两次

text = Label(myapp, text="Keyword: ").grid(row=0)

不起作用。你必须写:

 text = Label(myapp, text="Keyword: ")
 text.grid(row=0)