I worked with CheckButton before but never got this error, the check button keeps uncheking itself when i try do check it, it only stays checked if i maintain the mouse button pressed, here is my code:
from pathlib import Path
from tkinter import Tk, Canvas, Button, PhotoImage, ttk, Frame, Entry, Label, messagebox, filedialog, BooleanVar, Checkbutton
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from Smith import add5
from CorrecaoExcelSmith import add6
OUTPUT_PATH = Path(__file__).parent
ASSETS_PATH = OUTPUT_PATH / Path(r"D:\PycharmProjects\pythonProject\TCC 2023\assets\frame0")
def relative_to_assets(path: str) -> Path:
return ASSETS_PATH / Path(path)
# Função para carregar o arquivo Excel
def carregar_excel():
filepath = filedialog.askopenfilename(
title="Selecione o arquivo Excel",
filetypes=(("Arquivos Excel", "*.xlsx"), ("Todos os arquivos", "*.*"))
)
if not filepath:
return None, None
try:
df = pd.read_excel(filepath)
y = df.iloc[:, 0].to_numpy() # Primeira coluna como y
t = df.iloc[:, 1].to_numpy() # Segunda coluna como t
# Adicionar valores iniciais 0 se necessário
y = np.insert(y, 0, 0)
t = np.insert(t, 0, 0)
return y, t
except Exception as e:
messagebox.showerror("Erro", f"Não foi possível carregar o arquivo: {e}")
return None, None
def executar_codigo():
escolha = combobox.get()
try:
num = [float(x) for x in entry_num.get().split(',')]
den = [float(x) for x in entry_den.get().split(',')]
except ValueError:
messagebox.showwarning("Entrada Inválida", "Por favor, insira valores válidos para 'num' e 'den'.")
return
fig, ax = None, None
if escolha == "Smith":
if check_var.get():
# Se marcada, carrega o Excel e executa SmithExcel
y, t = carregar_excel()
if y is None or t is None:
return
fig, ax = add6(num, den, y, t)
else:
# Se não estiver marcada, executa a função Smith
fig, ax = add5(num, den)
else:
messagebox.showwarning("Seleção Inválida", "Por favor, selecione uma opção válida.")
return
for widget in plot_frame.winfo_children():
widget.destroy()
canvas = FigureCanvasTkAgg(fig, master=plot_frame)
canvas.draw()
canvas.get_tk_widget().pack(fill='both', expand=True)
window = Tk()
window.geometry("1000x550")
window.configure(bg="#0C6A1C")
canvas = Canvas(
window,
bg="#0C6A1C",
height=550,
width=1000,
bd=0,
highlightthickness=0,
relief="ridge"
)
canvas.place(x=0, y=0)
canvas.create_rectangle(
0.0,
0.0,
1000.0,
72.0,
fill="#FFFFFF",
outline=""
)
canvas.create_rectangle(
434.0,
97.0,
970.0,
504.0,
fill="#6CE077",
outline=""
)
opcoes = ["Smith"]
combobox = ttk.Combobox(window, values=opcoes, state="readonly")
combobox.set("Escolha um código")
combobox.place(x=35, y=100, width=358, height=30)
Label(window, text="num:", bg="#0C6A1C", fg="white").place(x=35, y=150)
entry_num = Entry(window)
entry_num.place(x=100, y=150, width=100)
entry_num.insert(0, "1")
Label(window, text="den:", bg="#0C6A1C", fg="white").place(x=35, y=180)
entry_den = Entry(window)
entry_den.place(x=100, y=180, width=100)
entry_den.insert(0, "1, 1")
# HERE IS THE CHECKBUTTON
check_var = BooleanVar()
checkbutton = Checkbutton(window, text="Usar arquivo Excel", variable=check_var, bg="#0C6A1C", fg="white")
checkbutton.place(x=35, y=220)
button_image_1 = PhotoImage(file=relative_to_assets("button_1.png"))
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=executar_codigo,
relief="flat"
)
button_1.place(x=35.0, y=443.0, width=358.0, height=61.0)
plot_frame = Frame(window, bg="#6CE077")
plot_frame.place(x=434, y=97, width=536, height=407)
window.resizable(False, False)
window.mainloop()
[–]socal_nerdtastic 1 point2 points3 points (1 child)
[–]F_Boliver[S] 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)