我正在尝试这样做,以便当我按下回车键时它会调用click()
功能,或者激活calculateButton
上的按键,我对我可怕的编码进行了apolagise,我是自学成才,所以它不以任何方式整洁或正确,但它有效......
代码:
import gi
import math
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class TableWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Injection Time Calculator")
table = Gtk.Table(7, 2, True)
self.add(table)
sizeLabel = Gtk.Label("WALL SIZE")
widthLabel = Gtk.Label("WIDTH")
heightLabel = Gtk.Label("HEIGHT")
fsLabel = Gtk.Label("FLAT STUD?")
ecLabel = Gtk.Label("EC?")
injLabel = Gtk.Label()
heightEntry=Gtk.Entry()
widthEntry=Gtk.Entry()
sizeBox = Gtk.ComboBoxText()
sizeBox.append("1","90")
sizeBox.append("2","140")
sizeBox.append("3","219")
fsCheckButton=Gtk.CheckButton()
ecCheckButton=Gtk.CheckButton()
calculateButton=Gtk.Button.new_with_label("CALCULATE INJECTION TIME")
calculateButton.connect("clicked", self.Click)
table.attach(sizeLabel, 0, 1, 0, 1)
table.attach(widthLabel, 0, 1, 1, 2)
table.attach(heightLabel, 0, 1, 2, 3)
table.attach(fsLabel, 0, 1, 3, 4)
table.attach(injLabel, 0, 2, 6, 7)
table.attach(ecLabel, 0, 1, 4, 5)
table.attach(calculateButton, 0, 2, 5, 6)
table.attach(widthEntry, 1, 2, 1, 2)
table.attach(heightEntry, 1, 2, 2, 3)
table.attach(sizeBox, 1, 2, 0, 1)
table.attach(fsCheckButton, 1, 2, 3, 4)
table.attach(ecCheckButton, 1, 2, 4, 5)
global sizeLabel
global heightEntry
global widthEntry
global fsCheckButton
global ecCheckButton
global sizeBox
global injLabel
def Click(self, calculateButton):
studWidth=sizeBox.get_active_text()
areaHeight=heightEntry.get_text()
areaWidth=widthEntry.get_text()
if fsCheckButton.get_active():
if studWidth=="90":
areaWidth=float(areaWidth)-59
elif studWidth=="140":
areaWidth=float(areaWidth)-38
elif studWidth=="219":
areaWidth=float(areaWidth)-24.3
if ecCheckButton.get_active():
if studWidth=="90":
areaWidth=float(areaWidth)-2*(59)
elif studWidth=="140":
areaWidth=float(areaWidth)-2*38
elif studWidth=="219":
areaWidth=float(areaWidth)-2*24.3
timeVar=float(studWidth)*10.7/140
injTime=((float(areaWidth)*float(areaHeight)*float(timeVar))/1000000)
a= str( 'INJECTION TIME IS '+str(injTime)[:7]+' SECONDS OR '+str((math.ceil(injTime*5))/5)+' ROUNDED')
injLabel.set_text(a)
fsCheckButton.set_active(False)
ecCheckButton.set_active(False)
win = TableWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
答案 0 :(得分:0)
Gtk.Window.set_default的文档告诉您如何设置默认情况下获得焦点的窗口小部件。当焦点在按钮上时按Enter键只会触发该按钮的clicked
信号。
通过鼠标或键盘激活是以相同的方式处理并且是透明的,所以当你已经将信号连接到它时,没有别的特别的事情要做。
答案 1 :(得分:0)
这假设您要在GtkEntry中按enter
来计算新数字:
import gi
import math
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class TableWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Injection Time Calculator")
table = Gtk.Table(7, 2, True)
self.add(table)
self.sizeLabel = Gtk.Label("WALL SIZE")
widthLabel = Gtk.Label("WIDTH")
heightLabel = Gtk.Label("HEIGHT")
fsLabel = Gtk.Label("FLAT STUD?")
ecLabel = Gtk.Label("EC?")
self.injLabel = Gtk.Label()
self.heightEntry=Gtk.Entry()
self.heightEntry.connect("activate", self.calculate)
self.widthEntry=Gtk.Entry()
self.widthEntry.connect("activate", self.calculate)
self.sizeBox = Gtk.ComboBoxText()
self.sizeBox.append("1","90")
self.sizeBox.append("2","140")
self.sizeBox.append("3","219")
self.fsCheckButton=Gtk.CheckButton()
self.ecCheckButton=Gtk.CheckButton()
calculateButton=Gtk.Button.new_with_label("CALCULATE INJECTION TIME")
calculateButton.connect("clicked", self.calculate)
table.attach(self.sizeLabel, 0, 1, 0, 1)
table.attach(widthLabel, 0, 1, 1, 2)
table.attach(heightLabel, 0, 1, 2, 3)
table.attach(fsLabel, 0, 1, 3, 4)
table.attach(self.injLabel, 0, 2, 6, 7)
table.attach(ecLabel, 0, 1, 4, 5)
table.attach(calculateButton, 0, 2, 5, 6)
table.attach(self.widthEntry, 1, 2, 1, 2)
table.attach(self.heightEntry, 1, 2, 2, 3)
table.attach(self.sizeBox, 1, 2, 0, 1)
table.attach(self.fsCheckButton, 1, 2, 3, 4)
table.attach(self.ecCheckButton, 1, 2, 4, 5)
def calculate (self, widget):
studWidth=self.sizeBox.get_active_text()
areaHeight=self.heightEntry.get_text()
areaWidth=self.widthEntry.get_text()
if self.fsCheckButton.get_active():
if studWidth=="90":
areaWidth=float(areaWidth)-59
elif studWidth=="140":
areaWidth=float(areaWidth)-38
elif studWidth=="219":
areaWidth=float(areaWidth)-24.3
if self.ecCheckButton.get_active():
if studWidth=="90":
areaWidth=float(areaWidth)-2*(59)
elif studWidth=="140":
areaWidth=float(areaWidth)-2*38
elif studWidth=="219":
areaWidth=float(areaWidth)-2*24.3
timeVar=float(studWidth)*10.7/140
injTime=((float(areaWidth)*float(areaHeight)*float(timeVar))/1000000)
a= str( 'INJECTION TIME IS '+str(injTime)[:7]+' SECONDS OR '+str((math.ceil(injTime*5))/5)+' ROUNDED')
self.injLabel.set_text(a)
self.fsCheckButton.set_active(False)
self.ecCheckButton.set_active(False)
win = TableWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
我已经改变了一些东西,使你的代码更好。