为清楚起见,我编辑了这篇文章。我想使用一个类从列表中生成按钮。在当前脚本中,我没有使用列表,只是使用变量app1实例化了该类,然后全屏显示了该信息:
如果取消注释列表= [1,2,3],并且取消注释def button():函数,则会出现空白屏幕。我怀疑使用“放置按钮网格”布局时,按钮会彼此重叠,因此我制作了“放置按钮填充”布局以用于测试。
我的最终目标是生成一个如下所示的GUI:
但是我知道我没有正确引用该类。我该怎么办?
import tkinter as tk
from tkinter import font
from tkinter import *
class alertButton(object):
def __init__(self, Canvas):
self.canvas=Canvas
myFont = font.Font(family='Helvetica', size=36, weight='bold')
#Configure Buttons
self.alertButton = Button(Canvas,text="Button 1",font=myFont, command=self.alertCycle, height=2, width=6)
self.quitButton = Button(canvas,text="Quit",font=myFont, command=canvas.quit, height=2, width=6)
#Place Buttons Pack
#self.alertButton.pack(padx=25, ipady=15, side=LEFT) # Pack from right to left
#self.quitButton.pack(padx=25, ipady=15, side=LEFT)
##This quit button only exists to exit full screen. Will remove later
#Place Buttons Grid
self.alertButton.grid(row=0,column=0,padx=20)
self.quitButton.grid(row=0,column=1,padx=20)
##This quit button only exists to exit full screen. Will remove later
def alertCycle(self):
print("Button Pressed")
#print (alertButton.i)
return
#list = [1,2,3]
#def button():
# for i in range(list):
# app[i]=alertButton(canvas)
root = tk.Tk()
root.title("Sensor GUI")
root.wm_attributes('-fullscreen', 'true')
canvas = tk.Canvas(root, bd=0, highlightthickness=0)
canvas.pack(fill=BOTH, expand=1)
app1 = alertButton(canvas)
root.mainloop()
答案 0 :(得分:0)
您可能可以设法将所有按钮置于一个循环内,以简化其创建过程。
AlertPanel
继承自tk.Frame
,AlertPanel
是容纳按钮集合的合适容器;因此本质上tk.Frame
是tk.Canvas
。尽管可以,但在已发布的代码中使用AlertPanel
似乎并不正确。tk.Buttons
正在管理Button 1
的集合,以及由每个按钮激活的方法表示的一系列动作。 2
和alert_cycle_1
绑定到2
和alert_cycle_3_to_8
;其余的make_alert_buttons
,您可以将其替换为希望每个按钮触发的适当操作。import tkinter as tk # <-- avoid star imports
from tkinter import font
class AlertPanel(tk.Frame): # <-- is a tk.Frame, and can be manipulated as such
def __init__(self, master):
self.master = master
super().__init__(self.master)
self.myFont = font.Font(family='Helvetica', size=36, weight='bold')
self.alert_actions = tuple([self.alert_cycle_1, self.alert_cycle_2] +\
[self.alert_cycle_3_to_8] * 6 +\
[self._quit])
self.alert_buttons = []
self.make_alert_buttons()
self.pack(fill=tk.BOTH, expand=True) # <-- because self is a tk.Frame
def make_alert_buttons(self):
btn_idx = 0
for row in range(3):
for col in range(3):
action = self.alert_actions[btn_idx]
b_text = f"Button {btn_idx + 1}"
if row == 2 and col == 2:
b_text = "Quit"
b = tk.Button(self,
text=b_text,
font=self.myFont,
command=action,
height=2, width=6)
b.grid(row=row, column=col, padx=20)
self.alert_buttons.append(b)
btn_idx += 1
self.alert_buttons = tuple(self.alert_buttons)
def alert_cycle_1(self):
print("Alert Button 1 Pressed")
def alert_cycle_2(self):
print("Alert Button 2 Pressed")
def alert_cycle_3_to_8(self): # <-- replace with appropriate action for buttons 3 to 8
# and update self.alert_actions accordingly
print("Alert Button 3 to 8 Pressed")
def _quit(self):
self.master.destroy()
root = tk.Tk()
root.title("Sensor GUI")
# root.wm_attributes('-fullscreen', 'true') # <-- untested
app1 = AlertPanel(root)
root.mainloop()
使用适当的文本创建每个按钮,并为其添加适当的操作,然后将按钮放置在面板上的位置。这给出了这样的内容:
AlertButtonPanel
[Edit]:再想一想,命名类sum = (eve) => {
var answer = $("#answer").val() ? parseInt($("#answer").val()) : 0;
var result = answer + parseInt(eve.target.value);
$("#answer").val(result)
}
function doSum() {
for (let i = 1; i < 6; i++) {
$("#" + i).click();
}
}
可能是一个更好的主意