【问题标题】:Simultaneous use of a checkbutton and a button同时使用一个检查按钮和一个按钮
【发布时间】:2017-07-15 18:26:25
【问题描述】:

我是 Python 新手,在使用检查按钮时显然遗漏了一些重要的东西。这是我的程序背后的想法:我手动选择一个文件,然后根据是否选中复选框,使用按钮触发一个计算序列或另一个。为此,我想使用 .get() 命令验证复选框的状态。

我发现只有一个序列始终被触发,与复选框的状态无关。当我点击复选框时,.get() 没有更新。我究竟做错了什么?任何帮助将不胜感激。

from tkinter import *
from tkinter import filedialog
import tkinter as tk

master = Tk()
root = tk.Tk()

col_sep = "\t"

col_h_b = []  # field column for background
col_m_b = []  # magnetization column for background

def choose_b_file():
    root.fileName_b = filedialog.askopenfilename(filetypes=((".dat files", "*.dat"), ("All files", "*.*")))
    with open(root.fileName_b, 'r') as f:
         for line in f:
            if line[0] != "#":
                linedata = [float(line.split(col_sep)[i]) for i in range(len(line.split(col_sep)))]
                col_h_b.append(linedata[4])
                col_m_b.append(linedata[5])
    print(f.name)

offset = BooleanVar()
checkbox = tk.Checkbutton(root, text="offset subtraction", variable=offset,onvalue=1, offvalue=0)
checkbox.pack()

def plot():
    if offset.get() == 1:
        #some mathematical operations and graph plotting
    else:
        #other mathematical operations and graph plotting

def close_window():
    exit()

b_data = Button(master, text="Background", width=20, command=choose_b_file)
m_minus_b = Button(master, text="Plot", width=5, command=plot)
quit = Button(master, text="Quit", width=5, command=close_window)

b_data.pack()
m_minus_b.pack()
quit.pack()

root.mainloop()

【问题讨论】:

    标签: python checkbox tkinter


    【解决方案1】:

    您主要是在搞乱父母的小部件rootmaster。 复选框需要单独的窗口吗?

    否则,快速解决方法是在创建复选框时将root 替换为master

    checkbox = tk.Checkbutton(root, text="offset subtraction" ...)
    

    你也可以简化布尔的东西,checkbutton的默认行为是使用0和1,去掉master或者root,只选择一个。

    from tkinter import *
    from tkinter import filedialog
    
    root = Tk()
    
    col_sep = "\t"    
    col_h_b = []  # field column for background
    col_m_b = []  # magnetization column for background
    
    def choose_b_file():
        root.fileName_b = filedialog.askopenfilename(filetypes=((".dat files", "*.dat"), ("All files", "*.*")))
        with open(root.fileName_b, 'r') as f:
             for line in f:
                if line[0] != "#":
                    linedata = [float(line.split(col_sep)[i]) for i in range(len(line.split(col_sep)))]
                    col_h_b.append(linedata[4])
                    col_m_b.append(linedata[5])
        print(f.name)
    
    def plot():
        if offset.get() == 1:
            print('True')
            #some mathematical operations and graph plotting
        else:
            print('False')
            #other mathematical operations and graph plotting
    
    def close_window():
        exit()
    
    offset = IntVar()
    checkbox = Checkbutton(root, text="offset subtraction", variable=offset)
    checkbox.pack()
    
    b_data = Button(root, text="Background", width=20, command=choose_b_file)
    m_minus_b = Button(root, text="Plot", width=5, command=plot)
    quit = Button(root, text="Quit", width=5, command=close_window)
    
    b_data.pack()
    m_minus_b.pack()
    quit.pack()
    
    root.mainloop()
    

    【讨论】:

    • 非常感谢@PrMoureu!那成功了!事实上,我也试图弄清楚如何在同一个窗口中显示复选框和按钮。所以你在一篇文章中解决了这两个问题:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2011-04-28
    相关资源
    最近更新 更多