我正在尝试使程序的背景色为#ff8080。但是我遇到了一些地方无法用该颜色填充的问题。
在单击“算术”或“几何”时,我还试图将退出按钮与中心对齐。
from tkinter import ttk
from tkinter import *
root = Tk()
root.resizable(0, 0)
root.title("Summing Series Calculator")
root.configure(bg='#ff8080')
root.configure(relief="sunken")
root.geometry("350x225")
root.overrideredirect(0)
v = IntVar()
titleFrame = Frame(root)
titleFrame.grid(row=0)
radioFrame = Frame(root)
radioFrame.grid(padx=50, pady=30)
inputFrame = Frame(root)
inputFrame.grid()
buttonFrame = Frame(root)
buttonFrame.grid()
Title = Label(titleFrame, bg='#ff8080', text="Summing Series", font="-weight bold")
Title.grid()
Label(radioFrame, bg='#ff8080', text="""Choose Either Arithmetic or Geometric""", justify = LEFT,padx = 20).grid()
def arith():
root = Tk()
root.resizable(0, 0)
root.title("Arithmetic Series Calculator")
root.configure(bg='#ff8080')
root.geometry('350x225')
def close():
root.destroy()
def Arithmetic():
global Result
try:
a = float(Inputa.get())
d = float(Inputd.get())
n = int(Inputn.get())
if n == 0:
Result = "Invalid Number of Terms"
elif d == 0:
Result = "Invalid Common Difference"
else:
Result = (n / 2) * ((2 * a) + (n - 1) * d)
except ValueError:
Result = "Input a Valid Number"
def calc():
if v.get() == 1:
Arithmetic()
else:
Arithmetic()
Output.config(state=NORMAL)
Output.delete(1.0, END)
Output.insert(INSERT, Result)
Output.config(state=DISABLED)
def clear():
Inputa.delete(0, END)
Inputn.delete(0, END)
Inputd.delete(0, END)
Output.config(state=NORMAL)
Output.delete(1.0, END)
Output.config(state=DISABLED)
titleFrame = Frame(root)
titleFrame.grid(row=0)
radioFrame = Frame(root)
radioFrame.grid(padx=10, pady=10)
inputFrame = Frame(root)
inputFrame.grid()
buttonFrame = Frame(root)
buttonFrame.grid()
outputFrame = Frame(root)
outputFrame.grid()
outputFrame = Frame(root)
outputFrame.grid()
outputFrame = Frame(root)
outputFrame.grid()
Title = Label(titleFrame, bg='#ff8080', text="Arithmetic Series", font="-weight bold")
Title.grid()
a = Label(inputFrame, bg='#ff8080', text="Enter The First Term: ", padx=10)
a.grid(row=0, column=0, sticky=E)
Inputa = Entry(inputFrame)
Inputa.grid(row=0, column=1, sticky=E)
d = Label(inputFrame, bg='#ff8080', text="Enter The Common Difference: ", padx=10)
d.grid(row=2, column=0, sticky=E)
Inputd = Entry(inputFrame)
Inputd.grid(row=2, column=1, sticky=E)
n = Label(inputFrame, bg='#ff8080', text="Number of Terms: ", padx=10)
n.grid(row=6, column=0, sticky=E)
Inputn = Entry(inputFrame)
Inputn.grid(row=6, column=1, sticky=E)
Comm = Label(inputFrame, bg='#ff8080', padx=5)
Comm.grid(row=8, column=0, sticky=E)
calculate = ttk.Button(buttonFrame, text="Calculate", command=calc)
calculate.grid(row=2, column=0, sticky=E + S)
Clear = ttk.Button(buttonFrame, text="Clear", command=clear)
Clear.grid(row=2, column=1, sticky=S)
close = ttk.Button(buttonFrame, text="Exit", command=close)
close.grid(row=15, column=1)
Output = Text(outputFrame, state=DISABLED, height=1, width=25)
Output.grid()
root.mainloop()
def geo():
root = Tk()
root.resizable(0, 0)
root.title("Geometric Series Calculator")
root.configure(bg='#ff8080')
root.geometry('350x225')
def close():
root.destroy()
def geometric():
global Result
try:
a = float(Inputa.get())
r = float(Inputr.get())
n = int(Inputn.get())
if r == 0:
Result = "Invalid Common Ratio"
elif n == 0:
Result = "Invalid Number Of Terms"
elif r > 1:
Result = a * (((r ** n) - 1) / (r - 1))
elif r == 1:
Result = a * n
else:
Result = a * ((1 - (r ** n)) / (1 - r))
except ValueError:
Result = 'Input a Valid Number'
def calc():
if v.get() == 1:
geometric()
else:
geometric()
Output.config(state=NORMAL)
Output.delete(1.0, END)
Output.insert(INSERT, Result)
Output.config(state=DISABLED)
def clear():
Inputa.delete(0, END)
Inputr.delete(0, END)
Inputn.delete(0, END)
Output.config(state=NORMAL)
Output.delete(1.0, END)
Output.config(state=DISABLED)
titleFrame = Frame(root)
titleFrame.grid(row=0)
radioFrame = Frame(root)
radioFrame.grid(padx=10, pady=10)
inputFrame = Frame(root)
inputFrame.grid()
buttonFrame = Frame(root)
buttonFrame.grid()
outputFrame = Frame(root)
outputFrame.grid()
outputFrame = Frame(root)
outputFrame.grid()
outputFrame = Frame(root)
outputFrame.grid()
Title = Label(titleFrame, bg='#ff8080', text="Geometric Series", font="-weight bold")
Title.grid()
a = Label(inputFrame, bg='#ff8080', text="Enter The First Term: ", padx=10)
a.grid(row=0, column=0, sticky=E)
Inputa = Entry(inputFrame)
Inputa.grid(row=0, column=1, sticky=E)
r = Label(inputFrame, bg='#ff8080', text="Enter The Common Ratio: ", padx=10)
r.grid(row=3, column=0, sticky=E)
Inputr = Entry(inputFrame)
Inputr.grid(row=3, column=1, sticky=E)
n = Label(inputFrame, bg='#ff8080', text="Number of Terms: ", padx=10)
n.grid(row=6, column=0, sticky=E)
Inputn = Entry(inputFrame)
Inputn.grid(row=6, column=1, sticky=E)
Comm = Label(inputFrame, padx=10)
Comm.grid(row=8, column=0, sticky=E)
calculate = ttk.Button(buttonFrame, text="Calculate", command=calc)
calculate.grid(row=2, column=0, sticky=E + S)
Clear = ttk.Button(buttonFrame, text="Clear", command=clear)
Clear.grid(row=2, column=1, sticky=S)
close = ttk.Button(buttonFrame, text="Exit", command=close)
close.grid(row=15, column=1)
Output = Text(outputFrame, state=DISABLED, height=1, width=25)
Output.grid()
root.mainloop()
calculate = ttk.Button(buttonFrame, text="Arithmetic", command=arith)
calculate.grid(row=2, column=0, sticky=E+S)
calculate = ttk.Button(buttonFrame, text="Geometric", command=geo)
calculate.grid(row=4, column=0, sticky=E+S)
root.mainloop()
当我单击“算术”或“几何”按钮时,它也会打开一个新窗口。有什么方法可以使它在单击这些按钮时停留在同一窗口上并更改为算术代码部分中的内容?
答案 0 :(得分:0)
要解决背景颜色问题,您需要更改Frame对象的背景。此外,使用ttk.Frame时存在问题。相反,请尝试以下操作:
import tkinter as tk # Add this line
...
inputFrame = tk.Frame(root, bg='#ff8080') # Add "tk." prefix to Frame(), and set color
inputFrame.grid()
查看How do I change the background of a Frame in Tkinter?的可接受答案
要更改按钮的对齐方式,请查看网格布局。如果您希望它看起来像这样button layout,请将网格布局选项更改为:
calculate = ttk.Button(buttonFrame, text="Calculate", command=calc)
calculate.grid(row=0, column=0, sticky=E+S) # Changed: row=0
Clear = ttk.Button(buttonFrame, text="Clear", command=clear)
Clear.grid(row=0, column=1, sticky=S) # Changed: row=0
close = ttk.Button(buttonFrame, text="Exit", command=close)
close.grid(row=1, column=0, columnspan=2)
# Changed: row=1, column=0 Added: columnspan=2
关于创建新窗口的按钮,您需要对程序进行很多更改。之所以打开新窗口,是因为您正在arith()和geo()方法中创建一个新的Tk()对象。
查看这篇文章以获取有关Tkinter中导航的更多信息:Using buttons in Tkinter to navigate to different pages of the application?
这是个人喜好,但我认为在使用Tkinter时更容易的选择是使用选项卡式ttk.Notebook来创建GUI的不同部分。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
notebook = ttk.Notebook(root)
startFrame = tk.Frame(notebook)
tk.Label(startFrame, text="Start Page...").pack()
arithmaticFrame = tk.Frame()
tk.Label(arithmaticFrame, text="Arithmatic Series...").pack()
geometricFrame = tk.Frame()
tk.Label(geometricFrame, text="Geometric Series...").pack()
notebook.add(startFrame, text="Start")
notebook.add(arithmaticFrame, text="Arithmatic")
notebook.add(geometricFrame, text="Geometric")
notebook.pack()
root.mainloop()