我如何展开输入按钮以桥接所有 3 行?即应该扩展到“0”“。”和 'Del'
how do i expand the enter button to bridge all 3 rows? i.e should be extended to sit under both '0' '.' and 'Del'
我正在使用 tkinter 创建我的第一个 GUI,但我遇到了一个问题。我如何展开小键盘的 'enter' 按钮(当您单击一个空框时打开)以覆盖当前那里的空 space?
如何指定小键盘的打开位置?
此外,如果有人有 shorter/faster 方法来编写此代码,我会对如何完成它感兴趣,因为虽然速度对我的项目来说不是非常必要,但拥有它总是很好。
代码如下:
import tkinter
from tkinter import *
from tkinter import simpledialog
def enumerate_row_column(iterable, num_cols):
for idx, item in enumerate(iterable):
row = idx // num_cols
col = idx % num_cols
yield row,col,item
class NumpadEntry(Entry):
def __init__(self,parent=None,**kw):
Entry.__init__(self,parent,**kw)
self.bind('<FocusIn>',self.numpadEntry)
self.bind('<FocusOut>',self.numpadExit)
self.edited = False
def numpadEntry(self,event):
if self.edited == False:
print("You Clicked on me")
self['bg']= '#ffffcc'
self.edited = True
new = numPad(self)
else:
self.edited = False
def numpadExit(self,event):
self['bg']= '#ffffff'
class numPad(simpledialog.Dialog):
def __init__(self,master=None,textVariable=None):
self.top = Toplevel(master=master)
self.top.protocol("WM_DELETE_WINDOW",self.ok)
self.createWidgets()
self.master = master
def createWidgets(self):
btn_list = ['1', '2', '3', '4', '5', '6', '7', '8', '0', '0', '.', 'Del', 'Enter']
# create and position all buttons with a for-loop
btn = []
# Use custom generator to give us row/column positions
for r,c,label in enumerate_row_column(btn_list,3):
# partial takes care of function and argument
cmd = lambda x = label: self.click(x)
# create the button
cur = Button(self.top, text=label, width=5, height=3, command=cmd)
# position the button
cur.grid(row=r, column=c)
btn.append(cur)
def click(self,label):
print(label)
if label == 'Del':
currentText = self.master.get()
self.master.delete(0, END)
self.master.insert(0, currentText[:-1])
elif label == 'Enter':
self.ok()
else:
currentText = self.master.get()
self.master.delete(0, END)
self.master.insert(0, currentText+label)
def ok(self):
self.top.destroy()
self.top.master.focus()
class App(Frame):
def __init__(self,parent=None,**kw):
Frame.__init__(self,parent,**kw)
self.textEntryVar1 = StringVar()
self.e1 = NumpadEntry(self,textvariable=self.textEntryVar1)
self.e1.grid()
self.textEntryVar2 = StringVar()
self.e2 = NumpadEntry(self,textvariable=self.textEntryVar2)
self.e2.grid()
if __name__ == '__main__':
root = Tk()
root.geometry("550x300")
root.title("Monitoring Site GUI")
app = App(root)
app.grid()
root.mainloop()
如评论所述:您需要使用 columnspan
在许多列中使用 space,并且 sticky
(west-east) 在此 space.
cur.grid(row=4, column=0, columnspan=3, sticky='we')
def createWidgets(self):
# create and position all buttons
btn = []
# --- without ENTER ---
btn_list = [
'1', '2', '3',
'4', '5', '6',
'7', '8', '0',
'0', '.', 'Del'
]
# Use custom generator to give us row/column positions
for r,c,label in enumerate_row_column(btn_list, 3):
# partial takes care of function and argument
cmd = lambda x = label: self.click(x)
# create the button
cur = Button(self.top, text=label, width=5, height=3, command=cmd)
# position the button
cur.grid(row=r, column=c)
btn.append(cur)
cur.grid(row=r, column=c)
# --- only ENTER ---
cmd = lambda x="Enter": self.click(x)
# create the button
cur = Button(self.top, text="Enter", width=5, height=3, command=cmd)
# position the button
cur.grid(row=4, column=0, columnspan=3, sticky='we')
btn.append(cur)
我正在使用 tkinter 创建我的第一个 GUI,但我遇到了一个问题。我如何展开小键盘的 'enter' 按钮(当您单击一个空框时打开)以覆盖当前那里的空 space?
如何指定小键盘的打开位置?
此外,如果有人有 shorter/faster 方法来编写此代码,我会对如何完成它感兴趣,因为虽然速度对我的项目来说不是非常必要,但拥有它总是很好。
代码如下:
import tkinter
from tkinter import *
from tkinter import simpledialog
def enumerate_row_column(iterable, num_cols):
for idx, item in enumerate(iterable):
row = idx // num_cols
col = idx % num_cols
yield row,col,item
class NumpadEntry(Entry):
def __init__(self,parent=None,**kw):
Entry.__init__(self,parent,**kw)
self.bind('<FocusIn>',self.numpadEntry)
self.bind('<FocusOut>',self.numpadExit)
self.edited = False
def numpadEntry(self,event):
if self.edited == False:
print("You Clicked on me")
self['bg']= '#ffffcc'
self.edited = True
new = numPad(self)
else:
self.edited = False
def numpadExit(self,event):
self['bg']= '#ffffff'
class numPad(simpledialog.Dialog):
def __init__(self,master=None,textVariable=None):
self.top = Toplevel(master=master)
self.top.protocol("WM_DELETE_WINDOW",self.ok)
self.createWidgets()
self.master = master
def createWidgets(self):
btn_list = ['1', '2', '3', '4', '5', '6', '7', '8', '0', '0', '.', 'Del', 'Enter']
# create and position all buttons with a for-loop
btn = []
# Use custom generator to give us row/column positions
for r,c,label in enumerate_row_column(btn_list,3):
# partial takes care of function and argument
cmd = lambda x = label: self.click(x)
# create the button
cur = Button(self.top, text=label, width=5, height=3, command=cmd)
# position the button
cur.grid(row=r, column=c)
btn.append(cur)
def click(self,label):
print(label)
if label == 'Del':
currentText = self.master.get()
self.master.delete(0, END)
self.master.insert(0, currentText[:-1])
elif label == 'Enter':
self.ok()
else:
currentText = self.master.get()
self.master.delete(0, END)
self.master.insert(0, currentText+label)
def ok(self):
self.top.destroy()
self.top.master.focus()
class App(Frame):
def __init__(self,parent=None,**kw):
Frame.__init__(self,parent,**kw)
self.textEntryVar1 = StringVar()
self.e1 = NumpadEntry(self,textvariable=self.textEntryVar1)
self.e1.grid()
self.textEntryVar2 = StringVar()
self.e2 = NumpadEntry(self,textvariable=self.textEntryVar2)
self.e2.grid()
if __name__ == '__main__':
root = Tk()
root.geometry("550x300")
root.title("Monitoring Site GUI")
app = App(root)
app.grid()
root.mainloop()
如评论所述:您需要使用 columnspan
在许多列中使用 space,并且 sticky
(west-east) 在此 space.
cur.grid(row=4, column=0, columnspan=3, sticky='we')
def createWidgets(self):
# create and position all buttons
btn = []
# --- without ENTER ---
btn_list = [
'1', '2', '3',
'4', '5', '6',
'7', '8', '0',
'0', '.', 'Del'
]
# Use custom generator to give us row/column positions
for r,c,label in enumerate_row_column(btn_list, 3):
# partial takes care of function and argument
cmd = lambda x = label: self.click(x)
# create the button
cur = Button(self.top, text=label, width=5, height=3, command=cmd)
# position the button
cur.grid(row=r, column=c)
btn.append(cur)
cur.grid(row=r, column=c)
# --- only ENTER ---
cmd = lambda x="Enter": self.click(x)
# create the button
cur = Button(self.top, text="Enter", width=5, height=3, command=cmd)
# position the button
cur.grid(row=4, column=0, columnspan=3, sticky='we')
btn.append(cur)