简体   繁体   English

使用按钮从 Tkinter 条目搜索框中获取文本

[英]Get text from Tkinter entry search box with a button

I'm pretty new to python and I just started with Tkinter.我对 python 很陌生,我刚开始使用 Tkinter。 I'm trying to make some self-exercise files.我正在尝试制作一些自我练习文件。 So far so good, but I went into a problem ( I will post the whole code and after I'll continue with the problem so that you can see what I want to make and where I do not understand how to do).到目前为止一切顺利,但我遇到了一个问题(我将发布整个代码,然后我将继续解决这个问题,以便您可以看到我想要做什么以及我不明白该怎么做的地方)。

#!/usr/bin/python

from tkinter import *
from PIL import Image, ImageTk
import subprocess



class Window(Frame):
 def __init__(self, master = None):
  Frame.__init__(self, master)
  self.master = master
  self.init_window()

 def init_window(self):
  self.master.title("ez-Installer")
  self.pack(fill=BOTH, expand=1)

  updateButton = Button(self, text="Update", command=self.system_update)
  updateButton.place(x=50, y=50)

  syncButton = Button(self, text="Sync packages", command=self.system_sync)
  syncButton.place(x=150, y=50)

  cmd1 = StringVar()
  mEntry = Entry(self,textvariable=cmd1).pack()

  installButton = Button(self, text="Install", command=self.system_install)
  installButton.place(x=50, y=150)

 def system_install(self):
  package = cmd1.get()
  install = "sudo pacman -S {} --noconfirm".format(package)
  subprocess.call([install], shell=True)

 def system_exit(self):
  exit()

 def system_update(self):
  subprocess.call(["sudo pacman -Su --noconfirm"], shell=True)

 def system_sync(self):
  subprocess.call(["sudo pacman -Syy --noconfirm"], shell=True)


root = Tk()
root.geometry("400x300")
app = Window(root)

root.mainloop()

The error is when pressing "Install" button.错误是在按下“安装”按钮时。 "cmd1 is not defined". “cmd1 未定义”。

def system_install(self):
 package = cmd1.get()
 install = "sudo pacman -S {} --noconfirm".format(package)
 subprocess.call([install], shell=True)

As you can see, I want it to get text from the search box Entry that I added here:如您所见,我希望它从我在此处添加的搜索框条目中获取文本:

  cmd1 = StringVar()
  mEntry = Entry(self,textvariable=cmd1).pack()

  installButton = Button(self, text="Install", command=self.system_install)
  installButton.place(x=50, y=150)

I know that my entry is under the def init_window(self): , but how can I grab the value of cmd1 from there?我知道我的条目在def init_window(self): ,但是如何从那里获取cmd1的值? Is it possible?是否可以? If not or if it is too much of a hassle, what would an similar alternative be?如果没有或者太麻烦,类似的选择是什么?

In your system_install method you have no access to the cmd1 variable as you did not attach it to the object instance.在您的system_install方法中,您无法访问cmd1变量,因为您没有将它附加到对象实例。 You just created it as a local variable in the init_window method.您刚刚在init_window方法中将其创建为局部变量。 To fix that, use self.cmd1 in each place to make it an instance variable that is visible to all method via the self argument.要解决这个问题, self.cmd1在每个地方使用self.cmd1使其成为一个实例变量,通过self参数对所有方法可见。

A separate problem is mEntry will be define to None as the pack() method does not return anything.一个单独的问题是mEntry将被定义为None因为pack()方法不返回任何内容。 I suspect what you mean to do there should be:我怀疑你的意思应该是:

self.cmd1 = StringVar()
self.mEntry = Entry(self,textvariable=self.cmd1)
self.mEntry.pack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM