[英]I can't use the Gtk.get-text instruction correctly in python
I'm writing a small program in Python with Gtk3 and this is part of the code: 我正在用Gtk3用Python编写一个小程序,这是代码的一部分:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Layout(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Ricerca partite")
self.set_default_size(265, 310)
self.connect("destroy", Gtk.main_quit)
layout = Gtk.Layout()
self.add(layout)
label = Gtk.Label("Inserisci i TAG della partita che cerchi")
layout.put(label, 10, 10)
xlabel = 10 ; ylabel = 50
xentry = 90 ; yentry = 45
yy=30
label = Gtk.Label(" Event ")
layout.put(label, xlabel, ylabel)
Event = Gtk.Entry()
Event.set_text("---test---")
layout.put(Event, xentry, yentry)
ylabel += yy ; yentry += yy
label = Gtk.Label(" Site ")
layout.put(label,xlabel, ylabel)
Site = Gtk.Entry()
layout.put(Site, xentry, yentry)
ylabel += yy ; yentry += yy
# ...
# Other 5 labels and entries
# ...
button = Gtk.Button(label=" cerca ")
layout.put(button, 186, ylabel+10)
button.connect("clicked", self.on_button_clicked)
txt=Event.get_text() # I verify "Event" entry
print(txt)
def on_button_clicked(self, button):
print("Button has been clicked!")
txt=window.Event.get_text()
print(ddd,type(ddd))
window = Layout()
window.show_all()
Gtk.main()
When the "Search" button is clicked I print "Button has been clicked"! 单击“搜索”按钮后,我将打印“已单击按钮”! and I would like to put in as many variables the content of the seven "entry" but, forgive my ignorance, despite all the trials, I can't get the result I want.
并且我想输入七个“ entry”(条目)内容的尽可能多的变量,但是,尽管经历了所有的尝试,我还是无知了,尽管如此,我还是无法获得想要的结果。
The error I receive is: 我收到的错误是:
Traceback (most recent call last):
File "scriptName.py", online 75, in on_button_clicked
txt=window.Event.get_text ()
AttributeError:' Layout' object has no attribute' Event'.
Thank you and apologize for the bad English: translated with PC. 谢谢,对于英语不好,我们深表歉意。
Translated with www.DeepL.com/Translator 翻译成www.DeepL.com/Translator
You may want to read up on Python classes sometime. 您可能需要在某个时候阅读Python类 。 Anyway, you were accessing a new class from inside the first class, leading to your error.
无论如何,您正在从第一个班级内部访问一个新班级,从而导致您的错误。 Here is the proper code:
这是正确的代码:
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Layout(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Ricerca partite")
self.set_default_size(265, 310)
self.connect("destroy", Gtk.main_quit)
layout = Gtk.Layout()
self.add(layout)
label = Gtk.Label("Inserisci i TAG della partita che cerchi")
layout.put(label, 10, 10)
xlabel = 10 ; ylabel = 50
xentry = 90 ; yentry = 45
yy=30
label = Gtk.Label(" Event ")
layout.put(label, xlabel, ylabel)
self.Event = Gtk.Entry()
self.Event.set_text("---test---")
layout.put(self.Event, xentry, yentry)
ylabel += yy ; yentry += yy
label = Gtk.Label(" Site ")
layout.put(label,xlabel, ylabel)
Site = Gtk.Entry()
layout.put(Site, xentry, yentry)
ylabel += yy ; yentry += yy
# ...
# Other 5 labels and entries
# ...
button = Gtk.Button(label=" cerca ")
layout.put(button, 186, ylabel+10)
button.connect("clicked", self.on_button_clicked)
txt=self.Event.get_text() # I verify "Event" entry
print(txt)
def on_button_clicked(self, button):
print("Button has been clicked!")
text=self.Event.get_text()
print(text,type(text))
window = Layout()
window.show_all()
Gtk.main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.