This is a follow-on to an answered post - I assume it's poor form to add to an answered thread even if it's related.
Overall I'm pretty happy with the layout (heavy lifting done by Bryan O. here).
Now I'm trying to fine-tune some widgets, and I can't seem to nudge things. In order to shift widgets I seem to need to layer additional frames to do so. This seems like using a shotgun to kill a fly, but what do I know?
I would like to nudge the button 'Add Edges" over so it has some space between it and the number entry widget to the left.
I would also love to have some space between the ok and cancel buttons on the bottom. Have tried adding padding via padx, and implementing layered frames whacks things up bad layout truncated central region pretty badly.I guess that geometry propagation means using padx isn't the right approach.
I cannot seem to nudge the widgets where I want them. My question specifically: using the code base I have, how do you recommend I make these fine-tune adjustments??
Thx
current gui layout
code:
from Tkinter import *
root2 = Tk()
root2.title('Model Definition')
root2.geometry('{}x{}'.format(460, 350))
# functions/commands
def get_list(event):
global seltext
"""
read the listbox selection and put the result somewhere
"""
# get selected line index
index = data_list.curselection()[0]
# get the line's text
seltext = data_list.get(index)
root2.update_idletasks()
# create all of the main containers
top_frame = Frame(root2, bg='cyan', width = 450, height=50, pady=6)
center = Frame(root2, bg='gray2', width=50, height=40, padx=3, pady=3)
btm_frame = Frame(root2, bg='plum4', width = 450, height = 45, pady=3)
btm_frame2_outer = Frame(root2, bg='lavender', width = 450, height = 60, pady=3)
btm_frame2 = Frame(btm_frame2_outer, bg='green', width = 350, height = 60, pady=3)
btm_frame2_cntr = Frame(btm_frame2_outer, bg='gray', width = 50, padx=7)
# layout all of the main containers
root2.grid_rowconfigure(1, weight=1)
root2.grid_columnconfigure(0, weight=1)
top_frame.grid(row=0, sticky="ew")
center.grid(row=1, sticky="nsew")
btm_frame.grid(row = 3, sticky="ew")
btm_frame2_outer.grid(row = 4, sticky="ew")
btm_frame2.grid(row = 1, columnspan = 2, sticky="ew")
btm_frame2_cntr.grid(row = 1, column = 4, sticky='ew')
# create the widgets for the top frame
model_label = Label(top_frame, text = 'Model Dimensions')
width_label = Label(top_frame, text = 'Width:')
length_label = Label(top_frame, text = 'Length:')
entry_W = Entry(top_frame, background="pink")
entry_L = Entry(top_frame, background="orange")
# layout the widgets in the top frame
model_label.grid(row = 0, column = 0, pady=5)
width_label.grid(row = 1, column = 0, sticky = 'e')
length_label.grid(row = 1, column = 2)
entry_W.grid(row = 1, column = 1)
entry_L.grid(row = 1, column = 3)
# create the center widgets
center.grid_rowconfigure(0, weight=1)
center.grid_columnconfigure(1, weight=1)
ctr_left = Frame(center, bg='blue', width=100, height=190)
ctr_mid = Frame(center, bg='yellow', width=250, height=190, padx=3, pady=3)
ctr_right = Frame(center, width=100, height=190, padx=3, pady=3)
ctr_left.grid(row=0, column = 0, sticky="ns")
ctr_mid.grid(row=0, column = 1, sticky="nsew")
ctr_right.grid(row=0, column = 2, sticky="ns")
# decorate the center frame widgets
# left
shift_up_label = Label(ctr_left, text = 'Shift Up')
shift_down_label = Label(ctr_left, text = 'Shift Down')
cut_label = Label(ctr_left, text = 'Cut')
copy_label = Label(ctr_left, text = 'Copy')
paste_label = Label(ctr_left, text = 'Paste')
# center
data_list = Listbox(ctr_mid, bg='snow2', width='55')
yscroll = Scrollbar(ctr_mid, command=data_list.yview, orient=VERTICAL)
# right
status_label = Label(ctr_right, text = 'Status', bg = 'green', height = 11)
#####################################################################
# layout the center widgets
#####################################################################
#left
shift_up_label.grid(row = 0, column = 0, pady = '7', sticky = 'nsew')
shift_down_label.grid(row = 1, column = 0, pady = '7', sticky = 'nsew')
cut_label.grid(row = 2, column = 0, pady = '7', sticky = 'nsew')
copy_label.grid(row = 3, column = 0, pady = '7', sticky = 'nsew')
paste_label.grid(row = 4, column = 0, pady = '7', sticky = 'nsew')
# center
data_list.grid(row=0, column=0, sticky='ns')
yscroll.grid(row=0, column=0, sticky='ens')
# right
status_label.grid(row = 2, column = 0, rowspan = 4, sticky = 'nsew')
# create the bottom widgets
# layout the bottom widgets
#####################################################################
# create bottom widgets
#####################################################################
label_label = Label(btm_frame, text = 'Label:', padx = '4')
entry_label = Entry(btm_frame, background="orange")
entry_number = Entry(btm_frame, background="cyan")
number_label = Label(btm_frame, text = 'Number:', padx = '4')
add_btn = Button(btm_frame, text='Add Edges', padx = '12')
ok_btn = Button(btm_frame2_cntr, text='OK', padx = '5')
cancel_btn = Button(btm_frame2_cntr, text='Cancel', padx = '12')
#####################################################################
# layout the bottom widgets
#####################################################################
label_label.grid(row = 1, column = 1, sticky = 'ew')
entry_label.grid(row = 1, column = 2, sticky = 'w')
number_label.grid(row = 1, column = 3, sticky = 'w')
entry_number.grid(row = 1, column = 4, sticky = 'e')
add_btn.grid(row = 1, column = 6, sticky = 'e')
ok_btn.grid(row = 0, column = 3, sticky = 'ew')
cancel_btn.grid(row = 0, column = 4, sticky = 'e')
# commands/bindings
data_list.configure(yscrollcommand=yscroll.set)
data_list.bind('<ButtonRelease-1>', get_list)
root2.mainloop()
I would like to nudge the button 'Add Edges" over so it has some space
between it and the number entry widget to the left.
When you call grid on a widget, there are two options that control spacing on either side of the widget: padx and pady. You can specify a single value that applies to both sides (eg: padx=20 will add 20 pixels on both the left and right side of the widget), or you can provide a two-tuple (eg: padx=(0,20) will only add 20 pixels on the right).
For example, to "nudge" the "Add Edges" button over, just add some padding:
add_btn.grid(row = 1, column = 6, sticky = 'e', padx=(20, 0))
I would also love to have some space between the ok and cancel buttons
on the bottom.
Again, padx is the solution:
ok_btn.grid(row = 0, column = 3, sticky = 'ew', padx=(0, 8))
Related
Application: Building a notes application (as an intro to GUI development in Python) which includes feature of a scrollbar to scroll through a textbox
Problem: I can't actually seem to scroll down through the textbox. I don't seem to get the grayed rectangle which lets me control the scrollbar and scroll up/down through the textbox
#importing necessary packages
from tkinter import *
from tkinter import font
from tkinter import ttk
#set up main window
root = Tk()
root.title("Notes")
root.geometry("400x650")
#functions
#functions to change all widget button's backgrounds when user hovers over it and leaves it
def enter_button(e):
e.widget.config(background = "#D4D4D4")
#SystemButtonFace is default colour
def leave_button(e):
e.widget.config(background = "SystemButtonFace")
#clear text in text-box
def clear():
#delete all text from text_box
text_box.delete(1.0,END)
def bold_it():
#create font
try:
bold_font = font.Font(text_box, text_box.cget("font"))
bold_font.configure(weight = "bold")
#creating tag called "bold" which bolds textll upon condition
text_box.tag_configure("bold", font = bold_font)
#creating a bold tag which highlights first character
bold_tag = text_box.tag_names("sel.first")
#condition for checking to see if tag is applied or not
#in the first highlighted character
#if tag is applied, remove the bold from first-highlighted text
#- last highlighted text
#"bold" needs to be matched in the tag
if "bold" in bold_tag:
text_box.tag_remove("bold","sel.first","sel.last")
else:
text_box.tag_add("bold","sel.first", "sel.last")
except TclError:
pass
def italics_it():
try:
#create a font
italics_font = font.Font(text_box, text_box.cget("font"))
italics_font.configure(slant = "italic")
#create a tag called "italic"
text_box.tag_configure("italics", font = italics_font)
italics_tag = text_box.tag_names("sel.first")
#condition to see whether tag has been applies or not
if "italics" in italics_tag:
text_box.tag_remove("italics", "sel.first","sel.last")
else:
text_box.tag_add("italics", "sel.first", "sel.last")
except TclError:
pass
#frames
top_frame = LabelFrame(root, padx = 30, pady = 10)
button_frame = LabelFrame(root, padx = 30, pady = 10)
text_frame = LabelFrame(root, padx = 10, pady = 10)
bottom_frame = LabelFrame(root, borderwidth = 0, highlightthickness = 5)
top_frame.grid(row = 0 , column = 0)
button_frame.grid(row = 1, column = 0, pady = 10)
text_frame.grid(row = 2, column = 0, pady = 1)
bottom_frame.grid(row = 3, column = 0, pady = 3)
#labels, textboxes, buttons
#top_frame content
Notes_label = Label(top_frame, text = "Notes", fg = "black", font = 1, padx = 141)
Notes_label.grid(row = 0 , column = 0)
save_button = Button(top_frame, text = "save")
#padx increases distance between buttons
#button_frame content
#bold button
#the ideal is that if u press ctrl + b, the bold_button is pressed by itself
#rn, it's gonna be a highlight technique
bold_button = Button(button_frame, text = "B", padx = 4, pady = 2, command = bold_it)
bold_button.grid(row = 0, column = 0)
#italicsize button
italics_button = Button(button_frame, text = "I", padx = 4, pady = 2, command = italics_it)
italics_button.grid(row = 0, column = 2, padx = 15)
#text_box frame button
text_box = Text(text_frame, width = 45, height = 27)
text_box.grid(row = 0, column = 0)
#text_box frame content
main_scrollbar = ttk.Scrollbar(text_frame, orient = "vertical", command = text_box.yview)
main_scrollbar.grid(row = 0, column = 1)
text_box["yscrollcommand"] = main_scrollbar.set
clear_button = Button(bottom_frame, text = "clear", padx = 2, pady = 2, command = clear)
clear_button.grid(row = 0, column = 0, padx = 15, pady = 10)
save_button = Button(bottom_frame, text = "save note", padx = 2, pady = 2)
save_button.grid(row = 0, column =1, padx = 15, pady = 10)
#binding all buttons for changing colours when user hovers over it and leaves it
bold_button.bind("<Enter>", enter_button)
bold_button.bind("<Leave>", leave_button)
italics_button.bind("<Enter>", enter_button)
italics_button.bind("<Leave>", leave_button)
clear_button.bind("<Enter>", enter_button)
clear_button.bind("<Leave>", leave_button)
save_button.bind("<Enter>", enter_button)
save_button.bind("<Leave>", leave_button)
# main program loop
root.mainloop()
here's an image of the problem image of problem
I would also be very grateful if one could explain the concept of scrollbar.set and like yview and why they are both needed for the scrollbar to work. Tutorials and videos don't seem to explain the concept, but just implement it
In line 145. You're missing sticky
main_scrollbar.grid(row = 0, column = 1, sticky=NS)
Output:
i want it to expand the size accordingly if the window is expanded, so far i use the grid.row/column configure to make its weight =1 but it will be all messed up it i expand the window.
import time
import tkinter as tk
#Initialise the window
clock = tk.Tk()
clock.title('Easy CLock')
clock.configure(bg='#121212')
clock.columnconfigure(0, weight = 1)
clock.rowconfigure(0, weight = 1)
border_effects = {
"flat": tk.FLAT,
"sunken": tk.SUNKEN,
"raised": tk.RAISED,
"groove": tk.GROOVE,
"ridge": tk.RIDGE,
}
#Logo will be under the main parent
logo = tk.PhotoImage(file = r'C:\Users\User\VSC\Alarm\Logo1.png')
logo_size = logo.subsample(5)
#Time and Date function
def time_date():
# current time
current_time = time.strftime('%H:%M:%S')
current_date = time.strftime(r'%m/%d/%Y')
clock.after(200, time_date)
#Displays the time
c_time = tk.Label(f_time, text = current_time, fg='white', bg='#121212', font=('Verdana', 30))
c_date = tk.Label(f_time, text = current_date, font=('Verdana', 10), fg='white', bg='#121212')
c_time.grid(column=0, row=0)
c_date.grid(column=0, row=1)
#alarm button command
def alarm_func():
#Alarm label
c_clicked = tk.Label(f_alarm, text='Alarm Interface', fg='white', bg='#121212')
c_clicked.grid(column=0, row=1, sticky = 'N')
def recall_frame(event):
if event == f_alarm:
event.grid_forget()
f_time.grid(column=0, row =1, columnspan = 4, sticky = 'N')
elif event == f_time:
event.grid_forget()
f_alarm.grid(column=0, row=1, columnspan = 4, rowspan = 2)
def back_func():
pass
#Creating Frames
f_time = tk.Frame(clock) #Clock Button
f_alarm = tk.Frame(clock) #Alarm Buttton
#configure the frames
f_time.configure(bg = '#121212')
f_alarm.configure(bg = '#121212')
#Setting label in the frame
f_lbl = tk.Label(clock, text= ' Simplistic Clock', image = logo_size, font=('Verdana', 30), fg='white', bg='#121212', compound = tk.LEFT)
time_but = tk.Button(clock, text='Clock', command= lambda :[time_date(), recall_frame(f_alarm)], bg='#f39c12', width = 15, relief = border_effects['ridge'])
alarm_but = tk.Button(clock, text = 'Alarm', command = lambda :[alarm_func(), recall_frame(f_time)], bg='#f39c12', width = 15, relief = border_effects['ridge'])
quit_but = tk.Button(clock, text='Exit', command = clock.quit, bg='#f39c12', width = 15, relief = border_effects['ridge'])
back_but = tk.Button(clock, text = 'Back To Home', command = back_func, bg='#f39c12', width = 15, relief = border_effects['ridge'])
f_lbl.config(borderwidth = 4, relief = border_effects['sunken'])
f_lbl.grid_columnconfigure(0, weight = 1)
#Putting it on the frames
f_lbl.grid(column = 0, row = 0, columnspan = 4, sticky = 'N')
time_but.grid(column = 0, row = 3)
alarm_but.grid(column = 1, row = 3)
back_but.grid(column = 2, row = 3)
quit_but.grid(column = 3, row = 3)
clock.mainloop()
also, why does the border in the f_lbl, simplistic clock not fully extended through out all 4 column since i put columnspan = 4 , and weight = 1 , should't it expand fully through all 4 columns?
I'm using Tkinter to create a GUI for my computer science coursework based on steganography. I'm using the .grid() function on the widgets in my window to lay them out, however I can't get this particular part to look how I want it to.
Here's what my GUI currently looks like: http://imgur.com/LNEZtEL
(or just the part with the error).
I want the remaining characters label to sit directly underneath the text entry box, but for some reason row 4 starts a large way down underneath the box. If I label the GUI with columns and rows anchored north west it looks like this: http://imgur.com/a/V7dTW.
If I shrink the image box on the left, it looks how I want, however I don't want the image this small: http://imgur.com/a/0Dudu.
The image box has a rowspan of 2, so what is causing the 4th row to start so low down from the text entry box? Here's roughly what I want the GUI to look like: http://imgur.com/a/ck04A.
Full code:
imageButton = Button(root, text="Add Image", command = add_image)
imageButton.grid(row = 2, columnspan = 2, sticky = W, padx = 30, pady = 20)
steg_widgets.append(imageButton)
image = Image.open("square.jpg")
image = image.resize((250,250))
photo = ImageTk.PhotoImage(image)
pictureLabel = Label(root, image = photo)
pictureLabel.image = photo
pictureLabel.grid(column = 0, row = 3, columnspan = 2, rowspan = 2, padx = 20, pady = (0, 20), sticky = NW)
steg_widgets.append(pictureLabel)
nameLabel = Label(root, text = "Brandon Edwards - OCR Computer Science Coursework 2016/2017")
nameLabel.grid(row = 0, column = 2, columnspan = 2, padx = (0, 20), pady = 10)
steg_widgets.append(nameLabel)
inputTextLabel = Label(root, text = "Enter text:")
inputTextLabel.grid(row = 2, column = 2, sticky = W)
steg_widgets.append(inputTextLabel)
startButton = Button(root, text="Go!", command = start_stega)
startButton.grid(row = 2, column = 2, sticky = E)
steg_widgets.append(startButton)
inputTextBox = Text(root, height = 10, width = 30)
inputTextBox.grid(row = 3, column = 2, sticky = NW)
steg_widgets.append(inputTextBox)
maxCharLabel = Label(root, text = "Remaining characters:")
maxCharLabel.grid(row = 4, column = 2, sticky = NW)
steg_widgets.append(maxCharLabel)
saveButton = Button(root, text="Save Image", command = save_image)
saveButton.grid(row = 2, column = 3, sticky = W)
steg_widgets.append(saveButton)
I recommend breaking your UI down into logical sections, and laying out each section separately.
For example, you clearly have two distinct sections: the image and button on the left, and the other widgets on the right. Start by creating containers for those two groups:
import Tkinter as tk
...
left_side = tk.Frame(root)
right_side = tk.Frame(root)
Since they are side-by-side, pack is the simplest way to lay them out:
left_side.pack(side="left", fill="y", expand=False)
right_side.pack(side="right", fill="both", expand=True)
Next, you can focus on just one side. You can use pack or grid. This uses grid for illustrative purposes:
image = tk.Canvas(left_side, ...)
button = tk.Button(left_side, ...)
left_side.grid_rowconfigure(0, weight=1)
left_side.grid_columnconfigure(0, weight=1)
image.grid(row=0, column=0, sticky="nw")
button.grid(row=1, column=0, sticky="n")
Finally, work on the right side. Since widgets are stacked top-to-bottom, pack is the natural choice:
l1 = tk.Label(right_side, text="Enter text:")
l2 = tk.Label(right_side, text="Remaining characters")
text = tk.Text(right_side)
l1.pack(side="top", fill="x")
text.pack(side="top", fill="both", expand=True)
l2.pack(side="top", fill="x")
I have built a gui for a script using tkinter.
I have tried building an executable with both cx_freeze and pyinstaller.
I make extensive use of scipy, numpy, statsmodels and matplotlib
Whenever I click the "Run" button, it spawns another window and the window beneath it stops responding. This can occur seemingly indefinitely.
I do use multiprocessing in my application, but I fixed the multiple windows issue in the python script version
How do I fix the multiple windows issue in my standalone program?
If there is not a reasonable fix, is there another way to package the program for use? Maybe a custom Python distribution?
from tkinter import *
from tkinter import ttk
def run():
import multiprocessing
import risers_fallers
import time
time_periods = list()
time_periods.append(month.get())
time_periods.append(threemonth.get())
time_periods.append(sixmonth.get())
time_periods.append(year.get())
#print(infile.get(), time_periods, filter_val.get(), outfile.get())
#loading screen
toplevel = Toplevel()
toplevel.focus_force()
loading = Label(toplevel, text = "RUNNING")#PhotoImage(file= photopath, format="gif - {}")
loading.pack()
subproc = multiprocessing.Process(target = risers_fallers.risers_fallers, args = (infile.get(), time_periods, filter_val.get(), outfile.get(),
top_x.get(), excel.get(), tableau.get(), onecat.get(), multicat.get(), external.get()))
subproc.start()
subproc.join()
toplevel.destroy()
def browsecsv():
from tkinter import filedialog
Tk().withdraw()
filename = filedialog.askopenfilename()
#print(filename)
infile.set(filename)
if __name__ == "__main__":
#initialize tk
root = Tk()
#set title
root.title("FM Risers and Fallers")
#create padding, new frame, configure columns and rows
#mainframe = ttk.Frame(root, padding="3 3 12 12")
#our variables
month = BooleanVar()
threemonth = BooleanVar()
sixmonth = BooleanVar()
year = BooleanVar()
excel = BooleanVar()
tableau = BooleanVar()
onecat = BooleanVar()
multicat = BooleanVar()
external = BooleanVar()
top_x = StringVar()
top_x.set("15")
infile = StringVar()
outfile = StringVar()
filter_val = StringVar()
photopath = "./41.gif"
#default values
filter_val.set("30")
import datetime
from re import sub
d = datetime.datetime.now()
outfile.set(sub('[^0-9|\s]',' ', str(d)))
"""
our widgets
"""
#labels for tab 1 Not needed with pane view
# ttk.Label(mainframe, text="Input file") #.grid(column=3, row=1, sticky=W)
# ttk.Label(mainframe, text="Output name") #.grid(column=3, row=1, sticky=W)
# ttk.Label(mainframe, text="Dashboard period of times") #.grid(column=3, row=1, sticky=W)
# ttk.Label(mainframe, text="Filter by growth rate") #.grid(column=3, row=1, sticky=W)
master = Frame(root, name = 'master')
master.pack(fill=BOTH)
#notebook container
notebook = ttk.Notebook(master)
notebook.pack(fill=BOTH, padx=2, pady=3)
tab0 = ttk.Frame(notebook)
tab1 = ttk.Frame(notebook)
tab2 = ttk.Frame(notebook)
notebook.add(tab0, text='Risers and Fallers')
notebook.add(tab1, text='Help')
notebook.add(tab2, text='About')
#tab 1 panes
panel_0 = ttk.Panedwindow(tab0, orient=VERTICAL)
file_pane_0 = ttk.Labelframe(panel_0, text='Input and Output', width = 300, height=100)
dashboard_pane_0 = ttk.Labelframe(panel_0, text='Dashboard Time Period', width = 300, height=200)
filter_pane_0 = ttk.Labelframe(panel_0, text='Filter by Growth Rate', width = 300, height=200)
run_frame_0 = Frame(panel_0, width = 300, height=100)
output_options_frame = ttk.Labelframe(panel_0, text='Output Options', width = 300, height=200)
top_x_frame_0 = ttk.Labelframe(panel_0, text = "Number of Risers", width = 300, height=100)
#grid it
panel_0.pack(fill=BOTH, expand=1)
file_pane_0.grid(row = 0, column = 0, columnspan = 3)
dashboard_pane_0.grid(row = 1, column = 0)
filter_pane_0.grid(row = 2, column = 0)
output_options_frame.grid(row = 1, column = 1)
top_x_frame_0.grid(row = 2, column = 1)
#pack em!
# panel_0.pack(fill=BOTH, expand=1)
# file_pane_0.pack(fill=X, expand=1)
# dashboard_pane_0.pack(side = LEFT, fill = Y)
# filter_pane_0.pack(side = RIGHT, fill = Y)
# top_x_frame_0.pack(fill=BOTH, expand=1)
#tab 2 panes
panel_1 = ttk.Panedwindow(tab1, orient=VERTICAL)
file_pane_1 = ttk.Labelframe(panel_1, text='Input and Output', width = 300, height=100)
dashboard_pane_1 = ttk.Labelframe(panel_1, text='Dashboard Time Period', width = 300, height=300)
filter_pane_1 = ttk.Labelframe(panel_1, text='Filter by Growth Rate', width = 300, height=300)
#pack em!
panel_1.pack(fill=BOTH, expand=1)
file_pane_1.pack(fill=BOTH, expand=1)
dashboard_pane_1.pack(side = LEFT, fill = Y)
filter_pane_1.pack(side = RIGHT, fill = Y)
#tab 3 panes
panel_2 = ttk.Panedwindow(tab2, orient=VERTICAL)
about_pane = ttk.Labelframe(panel_2, text='About', width = 300, height=200)
description_pane = ttk.Labelframe(panel_2, text='Description', width = 300, height=200)
citation_pane = ttk.Labelframe(panel_2, text='Citations', width = 300, height=200)
#pack em!
panel_2.pack(fill=BOTH, expand=1)
about_pane.pack(fill=BOTH, expand=1)
description_pane.pack(fill=BOTH, expand=1)
citation_pane.pack(fill=BOTH, expand=1)
#labels for tab 2 (help)
dashboard_help = ttk.Label(dashboard_pane_1, text= """Choose what units of
time to create a
dashboard for.
Note that time
periods are most
recent, not a
defined quarter or
calendar year.""")
io_help = ttk.Label(file_pane_1, text="""Output file: the first words to use in the naming of the output files
Input file: the BW output that you want to analyze.
See documentation for required format.""")
dashboard_help.pack()
io_help.pack(fill = BOTH)
#labels for tab 3 (about)
about_section = ttk.Label(about_pane, text="""The FM Risers and Fallers project was created by Jeremy Barnes
from May 2016 - July 2016 as a way to identify highest growing
parts.
Business logic was created based upon discussions with
Daniel DiTommasso, David Enochs, Alex Miles
and Robert Pietrowsky.
""")
description_section = ttk.Label(description_pane, text="""The FM Risers and Fallers application loads BW output from a
specific format, performs seasonal adjustment on the data,
derives information from the dataand then outputs all
derived information and dashboards with ranking information.
""")
citations_section = ttk.Label(citation_pane, text="""The FM Risers and Fallers project was created using
x13-ARIMA-SEATS by the US Census Bureau
pandas
statsmodels
Download links are available in the documentation
""")
#pack em
about_section.pack(fill=BOTH, expand=1)
description_section.pack(fill=BOTH, expand=1)
citations_section.pack(fill=BOTH, expand=1)
#file entry
#output
#output = Frame(file_pane_0, width = 300, height=50)
output_label = ttk.Label(file_pane_0, text="Output file").grid(column=1, row=1, sticky=W)
outfile_entry = ttk.Entry(file_pane_0, width = 50, textvariable = outfile).grid(column=2, row=1, sticky=W)
# output.pack(side = TOP, fill = X)
# output_label.pack(side = LEFT)
# outfile_entry.pack(side = RIGHT)
#input
#input = Frame(file_pane_0, width = 300, height=50)
input_label = ttk.Label(file_pane_0, text="Input file").grid(column=1, row=2, sticky=W)
infile_entry = ttk.Entry(file_pane_0, width = 50, textvariable = infile).grid(column=2, row=2, sticky=W)
#cbutton.grid(row=10, column=3, sticky = W + E)
bbutton= Button(file_pane_0, text="Browse", command= browsecsv).grid(column = 3, row = 2)
# input.pack(side = BOTTOM, fill = X)
# input_label.pack(side = LEFT)
# infile_entry.pack(side = RIGHT)
#top_x
top_x_label = ttk.Label(top_x_frame_0, text="Risers/Fallers to identify").grid(column=1, row=2, sticky=W)
top_x_entry = ttk.Entry(top_x_frame_0, width = 7, textvariable = top_x).grid(column=2, row=2, sticky=W)
#dashboard times
monthly = Checkbutton(dashboard_pane_0, text = "Month", variable = month).grid(row = 1, sticky=W, pady=1)
threemonthly = Checkbutton(dashboard_pane_0, text = "3 Months", variable = threemonth).grid(row = 2, sticky=W, pady=1)
sixmonthly = Checkbutton(dashboard_pane_0, text = "6 Months", variable = sixmonth).grid(row = 3, sticky=W, pady=1)
yearly = Checkbutton(dashboard_pane_0, text = "Year", variable = year).grid(row = 4, sticky=W, pady=1)
#output options
excel_button = Checkbutton(output_options_frame, text = "Excel", variable = excel).grid(row = 1, sticky=W, pady=1)
tableau_button = Checkbutton(output_options_frame, text = "Tableau", variable = tableau).grid(row = 2, sticky=W, pady=1)
onecat_button = Checkbutton(output_options_frame, text = "One Category", variable = onecat).grid(row = 3, sticky=W, pady=1)
multicat_button = Checkbutton(output_options_frame, text = "Many Categories", variable = multicat).grid(row = 4, sticky=W, pady=1)
external_button = Checkbutton(output_options_frame, text = "External Report", variable = external).grid(row = 5, sticky=W, pady=1)
#growth rate stuff
growth_input_label = ttk.Label(filter_pane_0, text="Analyze only top: ").grid(row = 1, column = 1, sticky=W, pady = 4, padx = 4)
growth_input = ttk.Entry(filter_pane_0, width = 7, textvariable = filter_val).grid(row = 1, column = 2, sticky=W, pady = 4, padx = 4)
# Radiobutton(filter_pane_0, text = "Standard Deviations", variable = stddev, value = 1).grid(row = 2, column = 1, sticky=W, pady = 4)
# Radiobutton(filter_pane_0, text = "Percentage", variable = stddev, value = 0).grid(row = 2, column = 2, sticky=W, pady = 4)
#growth_default_label = ttk.Label(filter_pane_0, text="(Leave blank for default").grid(row = 3, column = 1, sticky=W, pady = 4, padx = 4)
percent_label = ttk.Label(filter_pane_0, text=" %").grid(row = 1, column = 3, sticky=W, pady = 4, padx = 4)
#launch button
run_buttom_frame = Frame(panel_0, width = 300, height=50)
run_button = Button(run_buttom_frame, text = "RUN ANALYSIS", command = run)
run_button.pack()
run_buttom_frame.grid(row = 3, column = 0, columnspan = 2, pady = 4)
root.mainloop()
Two processes or more can't share a single root window. Every process that creates a widget will get its own window.
I managed to fix this with some experimentation.
It no longer launches multiple windows after I made the business logic launch within the same process.
I'm working on the GUI for a simple quiz app using Tkinter in Python 2.7.
Thus far, I have begun to set up my frame. I've put a scrollbar inside of a Text widget named results_txtbx to scroll up and down a list noting the player's performance on each question. I've been using grid since it's easier for me to manage.
from Tkinter import *
class Q_and_A:
def __init__(self, master):
frame = Frame(master)
Label(master).grid(row = 4)
results_txtbx = Text(master)
results_scrbr = Scrollbar(results_txtbx)
results_scrbr.grid(sticky = NS + E)
results_txtbx.config(width = 20, height = 4, wrap = NONE, yscrollcommand = results_scrbr.set)
results_txtbx.grid(row = 3, column = 1, padx = 12, sticky = W)
root = Tk()
root.wm_title("Question and Answer")
root.resizable(0, 0)
app = Q_and_A(root)
root.mainloop()
What happens is that when it runs, results_txtbx resizes to fit the scrollbar. Is there any way to make it keep its original size using grid?
You don't want to use a text widget as the master for a scrollbar. Like any other widget, if you pack or grid the scrollbar in the text widget, the text widget will shrink or expand to fit the scrollbar. That is the crux of your problem.
Instead, create a separate frame (which you're already doing), and use that frame as the parent for both the text widget and the scrollbars. If you want the appearance that the scrollbars are inside, set the borderwidth of the text widget to zero, and then give the containing frame a small border.
As a final usability hint, I recommend not making the window non-resizable. Your users probably know better what size of window they want than you do. Don't take that control away from your users.
Here's (roughly) how I would implement your code:
I would use import Tkinter as tk rather than from Tkinter import * since global imports are generally a bad idea.
I would make Q_and_A a subclass of tk.Frame so that it can be treated as a widget.
I would make the whole window resizable
I would separate widget creation from widget layout, so all my layout options are in one place. This makes it easier to write and maintain, IMO.
As mentioned in my answer, I would put the text and scrollbar widgets inside a frame
Here's the final result:
import Tkinter as tk
class Q_and_A(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master, borderwidth=1, relief="sunken")
self.label = tk.Label(self)
self.results_txtbx = tk.Text(self, width=20, height=4, wrap="none",
borderwidth=0, highlightthickness=0)
self.results_scrbr = tk.Scrollbar(self, orient="vertical",
command=self.results_txtbx.yview)
self.results_txtbx.configure(yscrollcommand=self.results_scrbr.set)
self.label.grid(row=1, columnspan=2)
self.results_scrbr.grid(row=0, column=1, sticky="ns")
self.results_txtbx.grid(row=0, column=0, sticky="nsew")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
root = tk.Tk()
root.wm_title("Question And Answer")
app = Q_and_A(root)
app.pack(side="top", fill="both", expand=True)
root.mainloop()
Set results_scrbr.grid(row = 3, column = 2) next to results_txtbx.grid(row = 3,column = 1, padx = 4), sticky is not needed because window is not resizable, and i lowered the padx so scrollbar is closer to text.
Also to make the results_txtbx vertically scrollable, add results_scrbr.config(command=results_txtbx.yview)
Here is a working code...
from Tkinter import *
class Q_and_A:
def __init__(self, master):
frame = Frame(master)
Label(master).grid(row = 4)
results_txtbx = Text(master)
results_scrbr = Scrollbar(master)
results_scrbr.grid(row = 3, column = 2)
results_scrbr.config(command=results_txtbx.yview)
results_txtbx.config(width = 20, height = 4,
wrap = NONE, yscrollcommand = results_scrbr.set)
results_txtbx.grid(row = 3, column = 1, padx = 4)
root = Tk()
root.wm_title("Question and Answer")
root.resizable(0, 0)
app = Q_and_A(root)
root.mainloop()
My implemented solution:
I needed to add more widgets to the app, so I bound the Scrollbar and Text widgets to another label and put that in the proper column the code (trimmed for readability) is below:
import Tkinter as tk
class Q_and_A(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.label = tk.Label(self)
#Set up menu strip
self.main_menu = tk.Menu(self)
self.file_menu = tk.Menu(self.main_menu, tearoff = 0)
self.file_menu.add_command(label = "Exit", command = self.quit)
self.main_menu.add_cascade(label = "File", menu = self.file_menu)
self.master.config(menu = self.main_menu)
#Set up labels
self.question_lbl = tk.Label(self, text = "Question #: ", padx = 12, pady = 6)
self.question_lbl.grid(row = 0, sticky = "w")
tk.Label(self, text = "Hint: ").grid(row = 1, sticky = "w", padx = 12, pady = 6)
tk.Label(self, text = "Answer: ").grid(row = 2, sticky = "w", padx = 12, pady = 6)
tk.Label(self, text = "Results: ").grid(row = 3, sticky = "nw", padx = 12, pady = 6)
tk.Label(self).grid(row = 4)
#Set up textboxes
self.question_txtbx = tk.Entry(self)
self.question_txtbx.config(width = 60)
self.question_txtbx.grid(row = 0, column = 1, padx = 12, columnspan = 3, sticky = "w")
self.help_txtbx = tk.Entry(self)
self.help_txtbx.config(width = 40)
self.help_txtbx.grid(row = 1, column = 1, columnspan = 2, padx = 12, sticky = "w")
self.answer_txtbx = tk.Entry(self)
self.answer_txtbx.config(width = 40)
self.answer_txtbx.grid(row = 2, column = 1, columnspan = 2, padx = 12, sticky = "w")
self.results_label = tk.Label(self)
self.results_txtbx = tk.Text(self.results_label, width = 10, height = 4, wrap = "none", borderwidth = 1, highlightthickness = 1)
self.results_scrbr = tk.Scrollbar(self.results_label, orient = "vertical", command = self.results_txtbx.yview)
self.results_txtbx.configure(yscrollcommand = self.results_scrbr.set)
self.label.grid(row = 1)
self.results_label.grid(row = 3, column = 1, padx = 11, sticky = "w")
self.results_scrbr.grid(row = 0, column = 1, sticky = "nse")
self.results_txtbx.grid(row = 0, column = 0, sticky = "w")
root = tk.Tk()
root.wm_title("Question and Answer")
#A note: The window is non-resizable due to project specifications.
root.resizable(0, 0)
app = Q_and_A(root)
app.pack(side = "top", fill = "both")
root.mainloop()
I'll keep storage in nested labels as a reference for myself for when I need to group things close together, unless there's some reason it should be avoided. Worked very well here. Thanks to Bryan for the advice.