I can't properly layout buttons within frame nested in tkk.Notebook
In Main.py I create ttk.Notebook and attach mainTab instance
root = tk.Tk()
rootFrame = tk.Frame(root, width=600, height=300)
rootFrame.grid(columnspan=1, rowspan=2)
rootFrame.pack(expand=1, fill="both")
tabs = ttk.Notebook(rootFrame)
tabs.grid(column=0, row=1, columnspan=1, rowspan=1)
mainTab = ttk.Frame(tabs)
mainTab.grid(columnspan=3, rowspan=6)
tabs.add(mainTab, text="Main")
rootFrame.pack(expand=1, fill="both")
mainPane = MainTab(root,mainTab)
root.mainloop()
in mainTab.py I'm trying to insert buttonFrame and layout two buttons within it
class MainTab:
...
def __init__(self, root, mainTab) -> None:
self.root = root
buttonFrame = tk.Frame(mainTab, bg="white")
buttonFrame.grid(column=0, row=4, columnspan=3, rowspan=1)
self.start_btn = tk.Button(buttonFrame, text="Start", command=lambda:self.start_timer(), font=BUTTON_FONT, bg="green", fg="white") # , height=1, width=14
self.start_btn.grid(column=0, row=0, columnspan=2)
self.reset_btn = tk.Button(buttonFrame, text="X", command=lambda:self.reset_timer(), font=BUTTON_FONT, bg="green", fg="white") # , height=1, width=1
self.reset_btn.grid(column=2, row=0)
...
As a result start button is not properly placed in the grid
It looks like buttonFrame takes full parent frame width and buttons placed in the middle regardless of their grid settings.
How I can properly layout buttons within buttonFrame?
Here is the requested "minimal reproducible example" which also look not good
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Time Tracker")
root.iconbitmap('./assets/logoTransp4icon24.ico')
rootFrame = tk.Frame(root, width=600, height=300)
rootFrame.grid(columnspan=1, rowspan=2)
rootFrame.pack(expand=1, fill="both")
tabs = ttk.Notebook(rootFrame)
tabs.grid(column=0, row=1, columnspan=1, rowspan=1)
mainTab = ttk.Frame(tabs)
mainTab.grid(columnspan=3, rowspan=6)
buttonFrame = tk.Frame(mainTab, bg="white")
buttonFrame.grid(column=0, row=4, columnspan=3, rowspan=1)
start_btn = tk.Button(buttonFrame, text="Start", command=lambda:self.start_timer(), font="Arial", bg="green", fg="white") # , height=1, width=14
start_btn.grid(column=0, row=0, columnspan=2)
reset_btn = tk.Button(buttonFrame, text="X", command=lambda:self.reset_timer(), font="Arial", bg="green", fg="white") # , height=1, width=1
reset_btn.grid(column=2, row=0)
timerDisplay = tk.Label(mainTab, text="00:00:00", font="Arial")
timerDisplay.grid(columnspan=2, column=1, row=4)
tabs.add(mainTab, text="Main")
rootFrame.pack(expand=1, fill="both")
root.mainloop()
buttonFrame occupies column 0 to 2 and timerDisplay occupies column 1 to 2. So timerDisplay overlaps buttonFrame. Removing columnspan=3 in buttonFrame.grid(...) can fix the overlapping issue:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Time Tracker")
root.iconbitmap('./assets/logoTransp4icon24.ico')
rootFrame = tk.Frame(root, width=600, height=300)
#rootFrame.grid(columnspan=1, rowspan=2) # override by below line
rootFrame.pack(expand=1, fill="both")
tabs = ttk.Notebook(rootFrame)
tabs.grid(column=0, row=1, rowspan=1)
mainTab = ttk.Frame(tabs)
#mainTab.grid(columnspan=3, rowspan=6) # not necessary
buttonFrame = tk.Frame(mainTab, bg="white")
buttonFrame.grid(column=0, row=4, rowspan=1) # removed columnspan=3
start_btn = tk.Button(buttonFrame, text="Start", command=lambda:self.start_timer(), font="Arial", bg="green", fg="white") # , height=1, width=14
start_btn.grid(column=0, row=0, columnspan=2)
reset_btn = tk.Button(buttonFrame, text="X", command=lambda:self.reset_timer(), font="Arial", bg="green", fg="white") # , height=1, width=1
reset_btn.grid(column=2, row=0)
timerDisplay = tk.Label(mainTab, text="00:00:00", font="Arial")
timerDisplay.grid(columnspan=2, column=1, row=4)
tabs.add(mainTab, text="Main")
#rootFrame.pack(expand=1, fill="both") # already called above
root.mainloop()
In the code given below, I want to create a GUI in such a way that 1) In the top frame: 1st row : "x"(checkbutton), right to "x"(checkbutton) must be "Choose xFile"(button) and right to "Choose xFile"(button) must be "clear"(button) and likewise in the 2nd row : for "y". Only when the "x" checkbutton is checked, the "choose xFile" button should gets enabled. And when clicked upon "choose xFile", it has to open the file dialogbox. And the choosen file contents should get displayed using "description box" below the "Input data"(label) in the middle frame(with both horizontal and vertical scrollbar). And when "clear" button is clicked, only the selected file contents(x or y file choosen) in the "description box" must be cleared and it must enable the "Choose xFile"(button) or "Choose yFile"(button) to perform the task again(i.e to open the file dialog box). Below to the "description box" must contain "Reset" button and to the right of "Reset" button must be "Submit" button in the middle portion of the middle frame. When the "Reset" button is clicked, all the contents being displayed in the "description box" must be cleared and the all the checkboxes must be unchecked so that the user can perform the selection process again. In the bottom frame, below "Model Output"(label), a "description box" along with "horizontal" and "vertical" scrollbar must be there. Below to the "description box" must contain "Exit" button which is placed in the middle of the "bottom frame".
from tkinter import *
def forButton1():
filename1 = askopenfilename()
with open(filename1) as f:
for i in f:
myList.insert(END, i)
print(filename1)
def forButton2():
filename1 = askopenfilename()
with open(filename1) as f:
for i in f:
myList.insert(END, i)
print(filename1)
def forButton7():
root.destroy()
root = Tk()
root.title("Spatialization of DSSAT")
topFrame = LabelFrame(root, text = "Select input file")
MyVar1 = IntVar()
MyVar2 = IntVar()
MyCheckbutton1 = Checkbutton(topFrame, text="x", variable=MyVar1)
#MyCheckbutton1.grid(row=0, column=0)
MyCheckbutton1.pack()
MyCheckbutton2 = Checkbutton(topFrame, text="y", variable=MyVar2)
#MyCheckbutton2.grid(row=1, column=0)
MyCheckbutton2.pack()
Button1 = Button(topFrame, text = "Choose xFile", command = forButton1)
#button1.grid(row=0, column=1)
Button1.pack()
Button2 = Button(topFrame, text = "Choose yFile", command = forButton2)
#button2.grid(row=0, column=1)
Button2.pack()
Button3 = Button(topFrame, text = "Clear")
Button3.pack()
Button4 = Button(topFrame, text = "Clear")
Button4.pack()
topFrame.pack(side=TOP)
middleFrame = Frame(root)
label1 = Label(middleFrame, text = "Input data:")
label1.grid(row = 4)
scrollbar = Scrollbar(middleFrame)
myList = Listbox(middleFrame, yscrollcommand = scrollbar.set)
myList.pack()
scrollbar.config( command = myList.yview )
scrollbar.pack()
Button5 = Button(middleFrame, text = "Reset")
#button1.grid(row=0, column=1)
Button5.pack()
Button6 = Button(middleFrame, text = "Submit")
#button1.grid(row=0, column=1)
Button6.pack()
middleFrame.pack()
bottomFrame = Frame(root)
label2 = Label(bottomFrame, text = "Model Output:")
label2.grid(row = 10)
Button7 = Button(bottomFrame, text = "Exit", command = forButton7)
#button1.grid(row=0, column=1)
Button7.pack()
bottomFrame.pack()
root.geometry("500x500")
root.mainloop()
Here I have fixed the placement of the given widgets (not their functionalities) as asked in question. You can follow this way to get your desired format:
from tkinter import *
from tkinter import filedialog
def forButton1():
filename1 = filedialog.askopenfilename()
with open(filename1) as f:
for i in f:
myList.insert(END, i)
print(filename1)
def forButton2():
filename1 = filedialog.askopenfilename()
with open(filename1) as f:
for i in f:
myList.insert(END, i)
print(filename1)
def forButton7():
root.destroy()
root = Tk()
root.title("Spatialization of DSSAT")
root.grid_columnconfigure(0, weight=1)
topFrame = LabelFrame(root, text="Select input file")
topFrame.grid(row=0, column=0, padx=8, pady=8, sticky=N+E+S+W)
topFrame.grid_rowconfigure(0, weight=1)
topFrame.grid_rowconfigure(1, weight=1)
topFrame.grid_columnconfigure(0, weight=1)
topFrame.grid_columnconfigure(1, weight=1)
topFrame.grid_columnconfigure(2, weight=1)
middleFrame = LabelFrame(root, text="Input data")
middleFrame.grid(row=1, column=0, padx=8, pady=8, sticky=N+E+S+W)
middleFrame.grid_rowconfigure(0, weight=1)
middleFrame.grid_rowconfigure(1, weight=0)
middleFrame.grid_columnconfigure(0, weight=1)
middleFrame.grid_columnconfigure(1, weight=1)
bottomFrame = LabelFrame(root, text="Model Output")
bottomFrame.grid(row=2, column=0, padx=8, pady=8, sticky=N+E+S+W)
bottomFrame.grid_rowconfigure(0, weight=1)
bottomFrame.grid_columnconfigure(0, weight=1)
MyVar1 = IntVar()
MyVar2 = IntVar()
MyCheckbutton1 = Checkbutton(topFrame, text="x", variable=MyVar1)
MyCheckbutton1.grid(row=0, column=0, padx=4, pady=4)
Button1 = Button(topFrame, text="Choose xFile", command=forButton1)
Button1.grid(row=0, column=1, padx=4, pady=4)
Button3 = Button(topFrame, text="Clear")
Button3.grid(row=0, column=2, padx=4, pady=4)
MyCheckbutton2 = Checkbutton(topFrame, text="y", variable=MyVar2)
MyCheckbutton2.grid(row=1, column=0, padx=4, pady=4)
Button2 = Button(topFrame, text="Choose yFile", command=forButton2)
Button2.grid(row=1, column=1, padx=4, pady=4)
Button4 = Button(topFrame, text="Clear")
Button4.grid(row=1, column=2, padx=4, pady=4)
innerMiddleFrame = Frame(middleFrame)
innerMiddleFrame.grid(row=0, column=0, columnspan=2, padx=4, pady=4)
innerMiddleFrame.grid_columnconfigure(0, weight=1)
innerMiddleFrame.grid_columnconfigure(1, weight=0)
scrollbar = Scrollbar(innerMiddleFrame)
myList = Listbox(innerMiddleFrame, yscrollcommand=scrollbar.set)
myList.grid(row=0, column=0, sticky=N+E+S+W)
scrollbar.config(command=myList.yview)
scrollbar.grid(row=0, column=1, sticky=N+E+S+W)
Button5 = Button(middleFrame, text="Reset")
Button5.grid(row=1, column=0, padx=4, pady=4)
Button6 = Button(middleFrame, text="Submit")
Button6.grid(row=1, column=1, padx=4, pady=4)
Button7 = Button(bottomFrame, text="Exit", command=forButton7)
Button7.grid(row=0, column=0, padx=4, pady=4)
root.geometry("250x425")
root.mainloop()
For my project, I need to create a GUI using Tkinter. I wanted to divide it into 4 different frames, each with different background colors. However, when I do this, only the first frame's background color is shown. I attached my code below as well as a screenshot of the output I'm getting.
from Tkinter import *
import tkMessageBox
root = Tk()
root.geometry("950x800")
root.configure(background="black")
def test():
print("this is a test")
# *****Frames*****
fileFrame = Frame(root)
fileFrame.configure(background="yellow")
fileFrame.pack()
attributeFrame = Frame(root)
attributeFrame.configure(background="red")
attributeFrame.pack()
constraintFrame = Frame(root)
constraintFrame.configure(background="purple")
constraintFrame.pack()
preferenceFrame = Frame(root)
preferenceFrame.configure(background="blue")
preferenceFrame.pack()
# *****File Frame*****
label_1 = Label(fileFrame, text="Enter Attributes file name:", anchor="e",
bg="red", font="Times 25", width=25, height=1)
label_2 = Label(fileFrame, text="Enter hard constraints file name:",
anchor="e", bg="blue", fg="yellow", font="Times 25", width=25, height=1)
label_3 = Label(fileFrame, text="Enter preferences file name:",
anchor="e", bg="purple", fg="green", font="times 25", width=25, height=1)
entry_1 = Entry(fileFrame, font="Times 25")
entry_2 = Entry(fileFrame, font="Times 25")
entry_3 = Entry(fileFrame, font="Times 25")
button_1 = Button(fileFrame, text="Submit", bg="red", font="Times 20")
button_2 = Button(fileFrame, text="Submit", bg="blue", fg="yellow",
font="Times 20")
button_3 = Button(fileFrame, text="Submit", bg="purple", fg="green",
font="Times 20")
label_1.grid(row=0, padx=5, pady=5)
entry_1.grid(row=0, column=1)
button_1.grid(row=0, column=2, padx=5, pady=5)
label_2.grid(row=1, padx=5, pady=5)
entry_2.grid(row=1, column=1)
button_2.grid(row=1, column=2, padx=5, pady=5)
label_3.grid(row=2, padx=5, pady=5)
entry_3.grid(row=2, column=1)
button_3.grid(row=2, column=2, padx=5, pady=5)
# *****Attribute Frame*****
label_Attribute_header = Label(attributeFrame, text="Attributes:",
bg="red", font="Times 25", width=25, height=1)
label_Attribute_header.pack()
# *****Constraint Frame*****
label_Constraint_header = Label(constraintFrame, text="Hard Constraints:",
bg="purple", fg="green", font="Times 25", width=25, height=1)
label_Constraint_header.pack()
# *****Preference Frame*****
label_Preference_header = Label(preferenceFrame, text="Preferences:",
bg="blue", fg="yellow", font="Times 25", width=25, height=1)
label_Preference_header.pack()
root.mainloop()
I expect there to be 4 different frames, all stacked on top of each other with different background colors, but instead only the first frame has a background color. Can somebody explain to me why this is and how I should go about fixing this? Thanks
Your frames are there. The bottom three frames have fewer things in them and you haven't given them any padding. The frame shrinks to fit, so when you just have one item, you won't see the frames.
You can easily see the frames if you do one of two things:
First, you can request that the frames fill their parent window in the x direction. When you do this, you'll see them:
fileFrame.pack(fill="x")
attributeFrame.pack(fill="x")
constraintFrame.pack(fill="x")
preferenceFrame.pack(fill="x")
Second, instead of or in addition to that, you can give padding around the labels in the bottom frames. That will let the frame colors appear.
label_Attribute_header.pack(padx=20, pady=20)
...
label_Constraint_header.pack(padx=20, pady=20)
...
label_Preference_header.pack(padx=20, pady=20)
I have been doing a python app for a calculator with Tkinter. I want to know how access the buttons properties such as their size, height, width and color by the use of strings. So far I have been able to change the background of the lines behind the buttons to red by using style.configure("TButton", background='red',
font="Serif 15",
padding=10 ).
How do I change the buttons themselves? If anyone could help me I would greatly appreciate it.
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
class Calculator:
calc_value = 0.0
div_trigger = False
mult_trigger = False
add_trigger = False
sub_trigger = False
def __init__(self,root):
self.entry_value = StringVar(root,value="")
root.title("Calculator")
root.geometry("600x500")
root.resizable(width=True, height=True)
style = ttk.Style()
style.configure(self, background='red')
style.configure("TButton", #background='red',
font="Serif 15",
padding=10 )
style.configure("TEntry",
font="Serif 15",
padding=10 )
self.number_entry = ttk.Entry(root,
textvariable=self.entry_value,width=50)
self.number_entry.grid(row=0, columnspan=4)
#-------1st row---------
self.button7 = ttk.Button(root, text="7",
command=lambda: self.button_press('7')).grid(row=1, column=0)
self.button8 = ttk.Button(root, text="8",
command=lambda: self.button_press('8')).grid(row=1, column=1)
self.button9 = ttk.Button(root, text="9",
command=lambda: self.button_press('9')).grid(row=1, column=2)
self.button_div = ttk.Button(root, text="/",
command=lambda: self.button_press('/')).grid(row=1, column=3)
#-------2nd row---------
self.button4 = ttk.Button(root, text="4",
command=lambda: self.button_press('4')).grid(row=2, column=0)
self.button5 = ttk.Button(root, text="5",
command=lambda: self.button_press('5')).grid(row=2, column=1)
self.button6 = ttk.Button(root, text="6",
command=lambda: self.button_press('6')).grid(row=2, column=2)
self.button_mult = ttk.Button(root, text="*",
command=lambda: self.button_press('*')).grid(row=2, column=3)
#-------3rd row---------
self.button1 = ttk.Button(root, text="1",
command=lambda: self.button_press('1')).grid(row=4, column=0)
self.button2 = ttk.Button(root, text="2",
command=lambda: self.button_press('2')).grid(row=4, column=1)
self.button3 = ttk.Button(root, text="3",
command=lambda: self.button_press('3')).grid(row=4, column=2)
self.button_add = ttk.Button(root, text="+",
command=lambda: self.button_press('+')).grid(row=4, column=3)
#-------4th row---------
self.button_clear = ttk.Button(root, text="AC",
command=lambda: self.button_press('AC')).grid(row=5, column=0)
self.button0 = ttk.Button(root, text="0",
command=lambda: self.button_press('0')).grid(row=5, column=1)
self.button_equal = ttk.Button(root, text="=",
command=lambda: self.button_press('=')).grid(row=5, column=2)
self.button_sub = ttk.Button(root, text="-",
command=lambda: self.button_press('-')).grid(row=5, column=3)
root = Tk()
calc = Calculator(root)
root.mainloop()
#button1 = Button(topframe,padx=16, pady=16, bd=8, text="1", fg="black", bg = random.choice(colors))
Here are documentatiopn about that.
http://effbot.org/tkinterbook/button.htm
If you want to change a botton size you can use.
button1.config(height = 100, width = 100)
So I'm writing a program for my final year project, and this thing in particular has just been bugging the hell out of me. I've been at it so long and I can't figure it out, so I'm hoping somebody fresh will be able to shed some light on it!
Basically, my program is for recording and storing results for my school's sports day. The idea is that it will actually be used in the future by the school, so I want it to be aesthetic as well as functional. Of course, this means integrating a UI of some kind, and since I've used Tkinter before it seemed like the obvious approach.
So far I've just got the basic windows coded. I have two windows: a main window and an "edit" window for changing participant names. I've written the code for these two windows in separate .py files and imported the "edit" window file into the main file, so that I can call for it to run when an "EDIT" button is clicked in the main window. For whatever reason, however, the "edit" window simply runs itself when I run the main program, even though the only reference to it is import editWindow.
This is the code for the main window:
'''
Created on 19 Jan 2015
#author: James.D.Wood
'''
#!/usr/bin/env python
#init
from tkinter import *
import editWindow
global event, year, cr, sr, a1, a2, s1, s2, d1, d2, n1, n2, ar1, ar2, sr1, sr2, dr1, dr2, nr1, nr2, msg, b, s, g
events = [
"70m hurdles",
"75m hurdles",
"80m hurdles",
"100m hurdles",
"100m",
"200m",
"300m",
"400m",
"800m",
"1500m",
"Javelin",
"Long-jump",
"High-jump",
"Triple-jump",
"Shot-put",
"Discus",
"Relay"
]
years = [
"7",
"8",
"9",
"10",
"11",
"12",
"13"
]
#create/define window
#main win
win = Tk()
win.title("Queens' Athletics")
win.minsize(800,600)
win.maxsize(800,600)
#section 1
event = StringVar() #event var
event.set("Select event...") #default
year = StringVar() #year var
year.set("Select year...") #default
lineImg = PhotoImage(file="resources/line.gif", master=win) #load separator image
sel = Label(win, text="Select event:")
slctEvnt = OptionMenu(win, event, *events) #dropdown box
slctEvnt.config(width=20)
selYr = Label(win, text="Select year:")
slctYr = OptionMenu(win, year, *years) #dropdown box
slctYr.config(width=20)
line = Label(win, image=lineImg) #separator
line.image = lineImg
#pack into window
sel.grid(row=0, column=0, sticky=W)
slctEvnt.grid(row=0, column=1, columnspan=4, sticky=W)
selYr.grid(row=1, column=0, sticky=W)
slctYr.grid(row=1, column=1, columnspan=4, sticky=W)
line.grid(row=2, column=0, columnspan=4, sticky=W, pady=15)
#section 2
cr = StringVar() #county record var
cr.set(" - ") #default
sr = StringVar() #school record var
sr.set(" - ") #default
crl = Label(win, text="County record:")
crBox = Label(win, width=6, textvariable=cr, state='normal', relief='sunk', bg="grey") #county record box
srl = Label(win, text="School record:")
srBox = Label(win, width=6, textvariable=sr, state='normal', relief='sunk', bg="grey") #school record box
#pack into window
crl.grid(row=3, column=0, sticky=W)
crBox.grid(row=3, column=1, sticky=W, padx=10)
srl.grid(row=3, column=2, sticky=W)
srBox.grid(row=3, column=3, sticky=W, padx=10)
#section 3
#participant names
a1 = StringVar()
a1.set(". . .") #default
a2 = StringVar()
a2.set(". . .") #default
s1 = StringVar()
s1.set(". . .") #default
s2 = StringVar()
s2.set(". . .") #default
d1 = StringVar()
d1.set(". . .") #default
d2 = StringVar()
d2.set(". . .") #default
n1 = StringVar()
n1.set(". . .") #default
n2 = StringVar()
n2.set(". . .") #default
#results
ar1 = StringVar()
ar2 = StringVar()
sr1 = StringVar()
sr2 = StringVar()
dr1 = StringVar()
dr2 = StringVar()
nr1 = StringVar()
nr2 = StringVar()
#
vLineImg = PhotoImage(file="resources/vline.gif", master=win) #load separator image
blank1 = Label(win, text="") #blank separator
part = Label(win, text="Students participating:")
resul = Label(win, text=" Results")
#participant names
auden1 = Label(win, width=36, textvariable=a1, state='normal', relief='sunk', bg="yellow")
auden2 = Label(win, width=36, textvariable=a2, state='normal', relief='sunk', bg="yellow")
suthe1 = Label(win, width=36, textvariable=s1, state='normal', relief='sunk', bg="blue")
suthe2 = Label(win, width=36, textvariable=s2, state='normal', relief='sunk', bg="blue")
drake1 = Label(win, width=36, textvariable=d1, state='normal', relief='sunk', bg="green")
drake2 = Label(win, width=36, textvariable=d2, state='normal', relief='sunk', bg="green")
newto1 = Label(win, width=36, textvariable=n1, state='normal', relief='sunk', bg="red")
newto2 = Label(win, width=36, textvariable=n2, state='normal', relief='sunk', bg="red")
#results
auRes1 = Entry(win, width=12, textvariable=ar1, justify=CENTER, state='normal', relief='sunk', bg="yellow")
auRes2 = Entry(win, width=12, textvariable=ar2, justify=CENTER, state='normal', relief='sunk', bg="yellow")
suRes1 = Entry(win, width=12, textvariable=sr1, justify=CENTER, state='normal', relief='sunk', bg="blue")
suRes2 = Entry(win, width=12, textvariable=sr2, justify=CENTER, state='normal', relief='sunk', bg="blue")
drRes1 = Entry(win, width=12, textvariable=dr1, justify=CENTER, state='normal', relief='sunk', bg="green")
drRes2 = Entry(win, width=12, textvariable=dr2, justify=CENTER, state='normal', relief='sunk', bg="green")
neRes1 = Entry(win, width=12, textvariable=nr1, justify=CENTER, state='normal', relief='sunk', bg="red")
neRes2 = Entry(win, width=12, textvariable=nr2, justify=CENTER, state='normal', relief='sunk', bg="red")
#
vLine = Label(win, image=vLineImg)
vLine.image = vLineImg
#pack into window
blank1.grid(row=5)
part.grid(row=6, column=0, columnspan=2, sticky=W, pady=8)
resul.grid(row=7, column=3, sticky=W)
#participant names
auden1.grid(row=8, column=0, columnspan=3, sticky=W, padx=8)
auden2.grid(row=9, column=0, columnspan=3, sticky=W, padx=8)
suthe1.grid(row=10, column=0, columnspan=3, sticky=W, padx=8)
suthe2.grid(row=11, column=0, columnspan=3, sticky=W, padx=8)
drake1.grid(row=12, column=0, columnspan=3, sticky=W, padx=8)
drake2.grid(row=13, column=0, columnspan=3, sticky=W, padx=8)
newto1.grid(row=14, column=0, columnspan=3, sticky=W, padx=8)
newto2.grid(row=15, column=0, columnspan=3, sticky=W, padx=8)
#results
auRes1.grid(row=8, column=3, columnspan=2, sticky=W)
auRes2.grid(row=9, column=3, columnspan=2, sticky=W)
suRes1.grid(row=10, column=3, columnspan=2, sticky=W)
suRes2.grid(row=11, column=3, columnspan=2, sticky=W)
drRes1.grid(row=12, column=3, columnspan=2, sticky=W)
drRes2.grid(row=13, column=3, columnspan=2, sticky=W)
neRes1.grid(row=14, column=3, columnspan=2, sticky=W)
neRes2.grid(row=15, column=3, columnspan=2, sticky=W)
#
vLine.grid(row=8, column=4, rowspan=8, sticky=W, padx=35)
#section 4
msg = StringVar()
msg.set("[ ]")
edit = Button(win, width=8, text="Edit >", padx=15, pady=8)
submit = Button(win, width=8, text="Submit >", padx=15, pady=8)
msgBox = Message(win, textvariable=msg, anchor=CENTER, justify=CENTER, aspect=200, borderwidth=2, relief='ridge', bg='grey')
reset = Button(win, width=8, text="Reset >", padx=15, pady=8)
#pack into window
edit.grid(row=16, column=1, rowspan=2, sticky=S, pady=12)
submit.grid(row=16, column=3, rowspan=2, sticky=S, pady=12)
msgBox.grid(row=16, column=4, rowspan=2, columnspan=3, sticky=S, pady=16)
reset.grid(row=16, column=7, rowspan=2, sticky=S, pady=12)
#section 5
img = PhotoImage(file="resources/logo.gif", master=win)
logo = Label(win, image=img)
logo.image = img
logo.grid(row=0, column=4, rowspan=7, columnspan=4, sticky=W+E+N+S, padx=60, pady=5)
#section 6
#leading participants
b = StringVar()
b.set(". . .") #default
s = StringVar()
s.set(". . .") #default
g = StringVar()
g.set(". . .") #default
stand = Label(win, text="Current standing:")
first = Label(win, text="1st")
secon = Label(win, text="2nd")
third = Label(win, text="3rd")
#leading participants
bron = Label(win, width=20, height=2, textvariable=b, state='normal', relief='sunk', bg="brown")
silv = Label(win, width=20, height=2, textvariable=s, state='normal', relief='sunk', bg="lightgrey")
gold = Label(win, width=20, height=2, textvariable=g, state='normal', relief='sunk', bg="gold")
#pack into window
stand.grid(row=7, column=5, rowspan=2, columnspan=3, sticky=E, padx=50)
first.grid(row=9, column=4, rowspan=2, sticky=E, padx=2)
secon.grid(row=11, column=4, rowspan=2, sticky=E, padx=2)
third.grid(row=13, column=4, rowspan=2, sticky=E, padx=2)
#leading participants
bron.grid(row=9, column=5, rowspan=2, columnspan=3, sticky=E, padx=22)
silv.grid(row=11, column=5, rowspan=2, columnspan=3, sticky=E, padx=22)
gold.grid(row=13, column=5, rowspan=2, columnspan=3, sticky=E, padx=22)
win.mainloop()
...And the code for the "edit" window:
'''
Created on 29 Jan 2015
#author: James.D.Wood
'''
#!/usr/bin/env python
#init
from tkinter import *
global whichEvent, ea1, ea2, es1, es2, ed1, ed2, en1, en2
#create/define window
#edit window
Tk().withdraw()
editWin = Toplevel()
editWin.title("Edit participants")
editWin.minsize(350,400)
editWin.maxsize(350,400)
#define elements
whichEvent = StringVar()
whichEvent.set("[EVENT]") #testing
editLineImg = PhotoImage(file="resources/sline.gif", master=editWin)
#edit names boxes
ea1 = StringVar()
ea2 = StringVar()
es1 = StringVar()
es2 = StringVar()
ed1 = StringVar()
ed2 = StringVar()
en1 = StringVar()
en2 = StringVar()
editing = Label(editWin, text="Editing for:")
editEvent = Label(editWin, width=30, textvariable=whichEvent, state='normal', relief='sunk', bg='grey')
editLine = Label(editWin, image=editLineImg) #separator
editLine.image = editLineImg
blank2 = Label(editWin, text="")
blank3 = Label(editWin, text="")
blank4 = Label(editWin, text="")
blank5 = Label(editWin, text="")
#house labels
labAud = Label(editWin, text="Auden:")
labSut = Label(editWin, text="Sutherland:")
labDra = Label(editWin, text="Drake:")
labNew = Label(editWin, text="Newton:")
#edit names boxes
editAud1 = Entry(editWin, width=30, textvariable=ea1, justify=CENTER, state='normal', relief='sunk', bg='yellow')
editAud2 = Entry(editWin, width=30, textvariable=ea2, justify=CENTER, state='normal', relief='sunk', bg='yellow')
editSut1 = Entry(editWin, width=30, textvariable=es1, justify=CENTER, state='normal', relief='sunk', bg='blue')
editSut2 = Entry(editWin, width=30, textvariable=es2, justify=CENTER, state='normal', relief='sunk', bg='blue')
editDra1 = Entry(editWin, width=30, textvariable=ed1, justify=CENTER, state='normal', relief='sunk', bg='green')
editDra2 = Entry(editWin, width=30, textvariable=ed2, justify=CENTER, state='normal', relief='sunk', bg='green')
editNew1 = Entry(editWin, width=30, textvariable=en1, justify=CENTER, state='normal', relief='sunk', bg='red')
editNew2 = Entry(editWin, width=30, textvariable=en2, justify=CENTER, state='normal', relief='sunk', bg='red')
#pack into window
editing.grid(row=0, column=0, sticky=W, pady=15)
editEvent.grid(row=0, column=1, sticky=W, pady=10)
editLine.grid(row=1, column=0, columnspan=3, sticky=W)
#house labels
blank2.grid(row=2, column=1, sticky=W)
labAud.grid(row=3, column=0, rowspan=2, sticky=W, pady=10)
blank3.grid(row=5, column=1, sticky=W)
labSut.grid(row=6, column=0, rowspan=2, sticky=W, pady=10)
blank4.grid(row=8, column=1, sticky=W)
labDra.grid(row=9, column=0, rowspan=2, sticky=W, pady=10)
blank5.grid(row=11, column=1, sticky=W)
labNew.grid(row=12, column=0, rowspan=2, sticky=W, pady=10)
#edit names boxes
editAud1.grid(row=3, column=1, sticky=W)
editAud2.grid(row=4, column=1, sticky=W)
editSut1.grid(row=6, column=1, sticky=W)
editSut2.grid(row=7, column=1, sticky=W)
editDra1.grid(row=9, column=1, sticky=W)
editDra2.grid(row=10, column=1, sticky=W)
editNew1.grid(row=12, column=1, sticky=W)
editNew2.grid(row=13, column=1, sticky=W)
I'm at the point now where I'm wondering if it would actually just be better to write the whole program in a single .py file, though I thought that doing it this way would be more elegant.
If somebody could suggest a nice, streamlined solution for this that would keep the code for the two windows separate, I would be very grateful. I'm using Python 3.4, FYI.
Thanks.
When you import a module, you are executing its code. That how importing modules actually works in python. To stop it, put code in the imported module in a function or a class. To be fair, its still being run to make the functions or classes, but they wont be called automatically. They will be just defined. In the main file, you just call the functions/class from the imported module.
All top-level statements in a python module are run when the file is imported. In your case, because the entirety of your edit file is at the module-level, every statement gets run upon import. I'd suggest taking your 'edit' commands and putting then inside a function:
def run_edit_window():
... put your code here
Then in your main file, you will import the edit file first:
import edit # Assuming file is named 'edit.py'
Then, when your edit button is pressed, call:
edit.run_edit_window()