我目前正在开发一个基于GUI的(Tkinter)项目,用于减少矩阵,仅用于练习。 在深入研究实际数学之前,我正在研究一个基本窗口,要求用户提供格式化矩阵的信息。
在一个领域,我有单选按钮 - 一个是“分数”,另一个是“十进制” - 他们可以选择输出格式。我想要的是,如果用户选择“十进制”,下面会出现另一个输入字段,以便他们可以输入要舍入的小数位数,如果他们选择“小数”,则该字段不会出现。
我在Stack Overflow上搜索了一下,看到了一些关于使用“raise()”和“lower()”方法的内容,但它现在还不能正常工作。如果你们可以提供任何意见,那就太棒了。关于GUI的一般反馈也很感激 - 我在学校学习了一段时间的Python,所以如果格式不好,请告诉我!
提前致谢!
编辑:我正在运行Python 3.4。
import tkinter
class Reducer:
'''
Takes user input of a matrix and row-reduces it to RREF.
GUI-Based interface that asks the user for the # of rows and columns, as well as output formatting.
The program can output as a fraction or a decimal with a specified number of decimals.
'''
def __init__(self):
'''
Initializes the GUI interface that asks the user for input.
No parameters or returns.
'''
self.main_window = tkinter.Tk()
#Initalize frames.
self.info_frame = tkinter.Frame()
self.size_frame = tkinter.Frame()
self.format_frame = tkinter.Frame()
self.digit_frame = tkinter.Frame()
self.button_frame = tkinter.Frame()
#Object for the information frame.
self.info_label = tkinter.Label(self.info_frame, text="Enter the number of rows and columns, your preferred output format,"
" and the number of trailing decimals if applicable.", justify="left",
wraplength=275).pack()
#Objects for the size frame - the matrix's rows/columns are set here.
self.row_label = tkinter.Label(self.size_frame, text="Rows:").pack(side="left")
self.row = tkinter.Entry(self.size_frame, width=3).pack(side="left")
self.col_label = tkinter.Label(self.size_frame, text="Columns:").pack(side="left")
self.col = tkinter.Entry(self.size_frame, width=3).pack(side="left")
#Objects for the digit frame
self.digit_label = tkinter.Label(self.digit_frame, text="Digits:")
self.digit = tkinter.Entry(self.digit_frame, width=3)
self.digit_label.pack(side="left")
self.digit.pack(side="left")
#Objects for the format frame - the output formatting is specified here.
self.output_var = tkinter.IntVar()
self.output_var.set(0)
self.fraction = tkinter.Radiobutton(self.format_frame, text="Fraction", variable=self.output_var, value=0,
command=self.hide_digits()).pack(side="left")
self.decimal = tkinter.Radiobutton(self.format_frame, text="Decimal", variable=self.output_var, value=1,
command=self.show_digits()).pack(side="left")
#Object for the bottom frame
self.button = tkinter.Button(self.button_frame, text="Next", command=self.reduce).pack()
#Pack frames.
self.info_frame.pack(anchor="nw")
self.size_frame.pack(anchor="nw")
self.format_frame.pack(anchor="nw")
self.digit_frame.pack(anchor="nw")
self.button_frame.pack(anchor="nw")
tkinter.mainloop()
def show_digits(self):
self.digit_label.lift(self.col)
self.digit.lift(self.col)
def hide_digits(self):
self.digit_label.lower(self.col)
self.digit.lower(self.col)
def reduce(self):
pass
reducer = Reducer()
答案 0 :(得分:1)
这是我之前提到的一个例子!这会产生一个新的输入字段(任何时候都有一个字段)。
import tkinter as tk
root = tk.Tk()
global num
num = 0
def create():
global entry
global num
if num == 0:
num = 1
entry = tk.Entry()
entry.pack()
def destroy():
global entry
global num
if num == 1:
num = 0
entry.destroy()
button1 = tk.Button(text = 'create entry', command = create)
button1.pack()
button2 = tk.Button(text = 'destroy entry', command = destroy)
button2.pack()
root.mainloop()
你也可以像这样禁用和启用输入字段,但是小部件总是在那里,所以它取决于你:
import tkinter as tk
root = tk.Tk()
global entry
entry = tk.Entry(state = 'disabled')
entry.pack()
def create():
global entry
entry.config(state = 'normal')
def destroy():
global entry
entry.config(state = 'disabled')
button1 = tk.Button(text = 'create entry', command = create)
button1.pack()
button2 = tk.Button(text = 'destroy entry', command = destroy)
button2.pack()
root.mainloop()
希望有所帮助。