如何限制按钮可以打开的窗口数?

我一般对tkinter和python都是陌生的。我正在申请,需要一个“设置”按钮。 这是我的创建方式:

@Entity @Qualifier("kpi")
public class TableSap {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long ids;

@Column(name = "ordem_vda")
private Long ordem_vda;

@Column(name = "org_vda")
private Long org_vda;

@Column(name = "canal_dist")
private Long canal_dist;

@Column(name = "setor_ativ")
private Long setor_ativ;

@Column(name = "escritorio")
private String escritorio;

@Column(name = "equipe")
private String equipe;

@Column(name = "tipo_doc")
private String tipo_doc;

@Column(name = "create_data")
private Date create_data;

@Column(name = "vlr_total_ordem")
private Double vlr_total_ordem;

@Column(name = "emissor_ordem")
private Long emissor_ordem;

@Column(name = "nome_emissor")
private String nome_emissor;

@Column(name = "repr")
private Long repr;

@Column(name = "nome_repr")
private String nome_repr;

public TableSap() {
    super();
}

public Long getIds() {
    return ids;
}

public void setIds(Long ids) {
    this.ids = ids;
}

public Long getOrdem_vda() {
    return ordem_vda;
}

public void setOrdem_vda(Long ordem_vda) {
    this.ordem_vda = ordem_vda;
}

public Long getOrg_vda() {
    return org_vda;
}

public void setOrg_vda(Long org_vda) {
    this.org_vda = org_vda;
}

public Long getcanal_dist() {
    return canal_dist;
}

public void setCanal_dist(Long canal_dist) {
    this.canal_dist = canal_dist;
}

public Long getSetor_ativ() {
    return setor_ativ;
}

public void setSetor_ativ(Long setor_ativ) {
    this.setor_ativ = setor_ativ;
}

public String getEscritorio() {
    return escritorio;
}

public void setEscritorio(String escritorio) {
    this.escritorio = escritorio;
}

public String getEquipe() {
    return equipe;
}

public void setEquipe(String equipe) {
    this.equipe = equipe;
}

public String getTipo_doc() {
    return tipo_doc;
}

public void setTipo_doc(String tipo_doc) {
    this.tipo_doc = tipo_doc;
}

public Date getcreate_data() {
    return create_data;
}

public void setCreate_data(Date create_data) {
    this.create_data = create_data;
}

public Double getVlr_total_ordem() {
    return vlr_total_ordem;
}

public void setVlr_total_ordem(Double vlr_total_ordem) {
    this.vlr_total_ordem = vlr_total_ordem;
}

public Long getEmissor_ordem() {
    return emissor_ordem;
}

public void setEmissor_ordem(Long emissor_ordem) {
    this.emissor_ordem = emissor_ordem;
}

public String getNome_emissor() {
    return nome_emissor;
}

public void setNome_emissor(String nome_emissor) {
    this.nome_emissor = nome_emissor;
}

public Long getRepr() {
    return repr;
}

public void setRepr(Long repr) {
    this.repr = repr;
}

public String getNome_repr() {
    return nome_repr;
}

public void setNome_repr(String nome_repr) {
    this.nome_repr = nome_repr;
}

其中:

buttonSettings = Button(win,text=txtSettings,command=lambda: create_window(win))
buttonSettings.grid(row=1,column=4)

如何将按钮可以创建的窗口数限制为一个?

longbp 回答:如何限制按钮可以打开的窗口数?

尝试此操作,您将需要添加window = None来初始化对象。

window = None

def create_window(win):
    if(not window):
        window = Toplevel(win)
,

如果您的目标是能够打开特定数量的窗口,请考虑将这些窗口存储在列表中,然后使用index()从存在的列表中弹出它们。

请参见以下示例,如果您有任何疑问,请告诉我:

import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('200x50')
        self.top_windows = []
        tk.Button(self,text='Open window!',command=self.open_top).pack()

    def open_top(self):
        if len(self.top_windows) <= 5:
            top = tk.Toplevel(self)
            self.top_windows.append(top)
            tk.Button(self.top_windows[-1],text='exit',command=lambda top=top: (self.top_windows.pop(self.top_windows.index(top)),top.destroy())).pack()

App().mainloop()
本文链接:https://www.f2er.com/3164833.html

大家都在问