【问题标题】:python print command in same open window not in prompt在同一打开窗口中的python打印命令不在提示中
【发布时间】:2015-04-11 16:13:37
【问题描述】:

好的,所以我正在尝试使用 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()

【问题讨论】:

    标签: python user-interface tkinter window


    【解决方案1】:

    你应该做的是在函数中创建一个标签(或者可能在之前创建并更新它)

    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)
    

    【讨论】:

    • 好的,所以我删除了双 tkinter 并定义了 show_entry_fields。
    • 你能给我一个创建标签的例子吗?作为我第一次使用 python,我不知道该怎么做。我也不确定这是否有助于将结果放在打开的窗口中。可以举个例子吗谢谢
    • text = Label(myapp, text="Keyword: ") text.grid(row= 4) 你把它放在 show_entry_fields 函数里面。
    • 1) 你有没有尝试用我的指示写一些东西? 2)它有效吗? 3) 发布您在问题结尾处写的内容以及结果。
    • 好的,Eric,我尝试了这个例子,但仍然没有这样做。我想我只是对使用 python 有点困惑,哈哈。去图...现在我的python应用程序代码如下,但就像我说的,我希望它在打开的窗口中发布结果,而不是像现在这样在命令行上发布结果..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2022-08-22
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多