Why is my tkinter Gui cut off on the right? - python

I just got into Python and have been using tkinter to design GUIs which has been fun so far. I've been experimenting with the frames, and I was trying to separate the screen into 3 'panes' if you will. For some reason, even though the combined widths are less than the total width, it still extends past the bounds of the screen.
For the life of me I can't figure out why the purple frame is cut off on the right.
I am somewhat suspicious of the amount of times I've used padx and pady. Also am curious if it's related to grid_propagate or pack_propagate, which is why I have used it so many times.
Any help is appreciated.
RESULT
import tkinter as tk
from tkinter import *
from tkinter import filedialog
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
def close():
exit()
#--------------------------------------------------------------
# Root Stuff
root = tk.Tk()
root.title('rough frame gui test v1')
root.geometry('1920x1080')
root.attributes('-fullscreen', False)
root.state('zoomed')
root.iconphoto(False, tk.PhotoImage(file='C:/Users/Trevor/OneDrive - Providence College/Research/Summer 2021/Single Py Files/spec_icon.png'))
#--------------------------------------------------------------
# A couple Random Functions
def donothing():
print('nothing')
def close():
root.destroy()
#---------------------------------------------------------------
# 0 - Main Frame
#root = Frame(root)
#root.grid(row=0, column=0, sticky="nswe")
#---------------------------------------------------------------
# 1 - Navigation Frame
frameNav = Frame(root, bg="blue", height=500, width=480)
frameNav.grid(row=0, column=0, sticky=N)
frameNav.grid_propagate(True)
global canvasNav
canvasNav = Canvas(frameNav, bg="white", height=500, width=480, bd=2, relief=SUNKEN)
canvasNav.grid(row=0, column=0, sticky="nswe", padx=5, pady=5)
canvasNav.grid_propagate(False)
navTitle = Label(canvasNav, bg='white', text="Current Database:", bd=2, anchor=CENTER)
navTitle.grid(row=0, column=0, padx=5, pady=5)
navPane = Label(canvasNav, bg='white', text="NAVIGATION PANE", bd=2, anchor=CENTER)
navPane.grid(row=2, column=0, padx=5, pady=5)
#---------------------------------------------------------------
# 2 - Graph Frame
frameGraph = Frame(root, bg="green", height=500, width=960)
frameGraph.grid(row=0, column=1, sticky=N)
frameGraph.grid_propagate(True)
global canvasGraph
canvasGraph = Canvas(frameGraph, bg="white", height=500, width=960, bd=2, relief=SUNKEN)
canvasGraph.grid(row=0, column=0, sticky="nswe", padx=5, pady=5)
canvasGraph.grid_propagate(False)
loadGraph = Button(canvasGraph, text="Load Graph", bd=2, anchor=CENTER)
loadGraph.grid(row=0, column=0, sticky=S, padx=5, pady=5)
#---------------------------------------------------------------
# 3 - Tasks Frame
frameTasks = Frame(root, bg="purple", height=500, width=400)
frameTasks.grid(row=0, column=2, sticky=N)
frameTasks.grid_propagate(True)
global bFrameTasks
bFrameTasks = Canvas(frameTasks, bg="white", height=500, width=400, bd=2, relief=SUNKEN)
bFrameTasks.grid(row=0, column=0, sticky="nswe", padx=5, pady=5)
bFrameTasks.grid_propagate(True)
#---------------------------------------------------------------
# FUNCTION TO HIDE WINDOWS (TASKS)
#
def toggleTasks():
try:
global bFrameTasks
print(bFrameTasks.winfo_exists())
if bFrameTasks.winfo_exists() == 1:
print("Destroying package...")
bFrameTasks.destroy()
else:
bFrameTasks = tk.Frame(frameTasks, bg="white", height=500, width=480, bd=2, relief=SUNKEN)
bFrameTasks.pack(padx=5, pady=5)
except Exception as e:
print(e)
print('An error has occurred...')
#---------------------------------------------------------------
#end
#---------------------------------------------------------------
# FUNCTION TO HIDE WINDOWS (GRAPH)
#
def toggleGraph():
try:
global canvasGraph
print(canvasGraph.winfo_exists())
if canvasGraph.winfo_exists() == 1:
print('Destroying package...')
canvasGraph.destroy()
else:
canvasGraph = tk.Canvas(frameGraph, bg="white", height=500, width=960, bd=2, relief=SUNKEN)
canvasGraph.pack(padx=5,pady=5)
except Exception as e:
print(e)
print('An error has occurred...')
#---------------------------------------------------------------
#end
#---------------------------------------------------------------
# FUNCTION TO HIDE WINDOWS (NAVIGATION)
#
def toggleNav():
try:
global canvasNav
print(canvasNav.winfo_exists())
if canvasNav.winfo_exists():
print('Destroying...')
canvasNav.destroy()
else:
canvasNav = tk.Canvas(frameNav, bg="white", height=500, width=480, bd=2, relief=SUNKEN)
canvasNav.pack(padx=5,pady=5)
except Exception as e:
print(e)
print('An error has occurred')
#---------------------------------------------------------------
#end
def fileExplore():
dbFolder = filedialog.askdirectory(initialdir = '/', title = 'Select Database Folder')
print(dbFolder) #working! just need to get it to display to a widget
folderName = tk.Label(canvasNav, bg='gray', text=dbFolder)
folderName.grid(row=1, column=0, sticky=N, padx=5, pady=5)
#---------------------------------------------------------------
# MENU
menuBar = Menu(root)
fileMenu = Menu(menuBar, tearoff=0)
menuBar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Open Database Folder", command=fileExplore)
fileMenu.add_command(label="Save", command=donothing)
fileMenu.add_command(label="Save as...", command=donothing)
fileMenu.add_separator()
fileMenu.add_command(label="Exit...", command=close)
hideMenu = Menu(menuBar, tearoff=0)
menuBar.add_cascade(label="Hide", menu=hideMenu)
hideMenu.add_checkbutton(label="Task Bar", command=toggleTasks)
hideMenu.add_checkbutton(label="Graph", command=toggleGraph)
hideMenu.add_checkbutton(label="Navigation", command=toggleNav)
#---------------------------------------------------------------
root.config(menu=menuBar)
root.mainloop()

I think Python's not yet aware of the correct screen resolution even though you're setting it with geometry(). Based on :
When should I use root.update() in tkInter for python
Why does the "geometry()" method work with a delay? :
I'd say add the line for root.update_idletasks() after the root.state('zoomed') to see if that helps to change it.
If you don't want to hardcode the geometry each time you can try root.geometry(f"{root.winfo_width()}x{root.winfo_height()}". I think you may want to adjust the widths/height of the other Frames after this as well in consideration of borders taking up some pixels and the space the menu bar would take on the height

Related

Navigating through pages in customtkinter python?

I feel like my question is pretty straightforward, but I just cant seem to figure out how to do this. I'm currently creating a GUI project with CustomTkInter and trying to make a button that navigates through to the next page, but I'm not sure how to do this? I'm trying to link different pages together, essentially just trying to have a multi-page project, I've tried to apply the solution for the normal tkinter to my customtkinter - but its not worked so far and its throwing me some errors (specifically an error about a master not being defined)
This is what I have so far
import tkinter
import tkinter.messagebox
import customtkinter
from PIL import Image
import os
customtkinter.set_appearance_mode("Dark") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("green") # Themes: "blue" (standard), "green", "dark-blue"
class Page(customtkinter.CTkFrame):
def __init__(self):
customtkinter.CTkFrame.__init__()
def show(self):
self.lift()
class Page1(Page):
def __init__(self):
Page.__init__()
class HomePage(customtkinter.CTkFrame):
def __init__(self):
super().__init__()
self.title("IMS")
self.geometry(f"{1300}x{800}")
self.rowconfigure((0), weight=1)
self.columnconfigure((0), weight=1)
self.rowconfigure((1, 2), weight=1)
self.columnconfigure((1,2), weight=1)
self.rowconfigure((3), weight=1)
image_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images")
self.logoImage = customtkinter.CTkImage(Image.open(os.path.join(image_path, "logo.png")), size=(150, 66))
self.titleLogo = customtkinter.CTkLabel(master=self, text="", image=self.logoImage)
self.titleLogo.grid(row=0, column=1, padx=0, pady=0)
self.categoriesButton = customtkinter.CTkButton(master=self, border_width=2, text_color=("gray10", "#DCE4EE"),font=("",33), width=150, height=50, text="Categories", command=Page1.show())
self.categoriesButton.grid(row=2, column=0, padx=(50, 50), pady=(0, 40), sticky="nsew", columnspan=3)
if __name__ == "__main__":
app = HomePage()
app.mainloop()
Would appreciate any help, ty :)
here's a great CTk doc that could help with your problem https://felipetesc.github.io/CtkDocs/#/multiple_frames
i made some minor changes because i was getting some error running sample 1 with python 3.9.13
import tkinter
import customtkinter
DARK_MODE = "dark"
customtkinter.set_appearance_mode(DARK_MODE)
customtkinter.set_default_color_theme("dark-blue")
class App(customtkinter.CTk):
frames = {"frame1": None, "frame2": None}
def frame1_selector(self):
App.frames["frame2"].pack_forget()
App.frames["frame1"].pack(in_=self.right_side_container,side=tkinter.TOP, fill=tkinter.BOTH, expand=True, padx=0, pady=0)
def frame2_selector(self):
App.frames["frame1"].pack_forget()
App.frames["frame2"].pack(in_=self.right_side_container,side=tkinter.TOP, fill=tkinter.BOTH, expand=True, padx=0, pady=0)
def __init__(self):
super().__init__()
# self.state('withdraw')
self.title("Change Frames")
self.geometry("{0}x{0}+0+0".format(self.winfo_screenwidth(), self.winfo_screenheight()))
# contains everything
main_container = customtkinter.CTkFrame(self)
main_container.pack(fill=tkinter.BOTH, expand=True, padx=10, pady=10)
# left side panel -> for frame selection
left_side_panel = customtkinter.CTkFrame(main_container, width=150)
left_side_panel.pack(side=tkinter.LEFT, fill=tkinter.Y, expand=False, padx=10, pady=10)
# buttons to select the frames
bt_frame1 = customtkinter.CTkButton(left_side_panel, text="Frame 1", command=self.frame1_selector)
bt_frame1.grid(row=0, column=0, padx=20, pady=10)
bt_frame2 = customtkinter.CTkButton(left_side_panel, text="Frame 2", command=self.frame2_selector)
bt_frame2.grid(row=1, column=0, padx=20, pady=10)
# right side panel -> to show the frame1 or frame 2
self.right_side_panel = customtkinter.CTkFrame(main_container)
self.right_side_panel.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True, padx=10, pady=10)
self.right_side_container = customtkinter.CTkFrame(self.right_side_panel,fg_color="#000811")
self.right_side_container.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True, padx=0, pady=0)
App.frames['frame1'] = customtkinter.CTkFrame(main_container,fg_color="red")
bt_from_frame1 = customtkinter.CTkButton(App.frames['frame1'], text="Test 1", command=lambda:print("test 1") )
bt_from_frame1.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
App.frames['frame2'] = customtkinter.CTkFrame(main_container,fg_color="blue")
bt_from_frame2 = customtkinter.CTkButton(App.frames['frame2'], text="Test 2", command=lambda:print("test 2") )
bt_from_frame2.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
a = App()
a.mainloop()
i made a very basic template that you could use and modify as you want or just take the principle out of it.
import tkinter
import customtkinter
DARK_MODE = "dark"
customtkinter.set_appearance_mode(DARK_MODE)
customtkinter.set_default_color_theme("dark-blue")
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.title("Change Frames")
# remove title bar , page reducer and closing page !!!most have a quit button with app.destroy!!! (this app have a quit button so don't worry about that)
self.overrideredirect(True)
# make the app as big as the screen (no mater wich screen you use)
self.geometry("{0}x{1}+0+0".format(self.winfo_screenwidth(), self.winfo_screenheight()))
# root!
self.main_container = customtkinter.CTkFrame(self, corner_radius=10)
self.main_container.pack(fill=tkinter.BOTH, expand=True, padx=10, pady=10)
# left side panel -> for frame selection
self.left_side_panel = customtkinter.CTkFrame(self.main_container, width=150, corner_radius=10)
self.left_side_panel.pack(side=tkinter.LEFT, fill=tkinter.Y, expand=False, padx=5, pady=5)
self.left_side_panel.grid_columnconfigure(0, weight=1)
self.left_side_panel.grid_rowconfigure((0, 1, 2, 3), weight=0)
self.left_side_panel.grid_rowconfigure((4, 5), weight=1)
# self.left_side_panel WIDGET
self.logo_label = customtkinter.CTkLabel(self.left_side_panel, text="Welcome! \n", font=customtkinter.CTkFont(size=20, weight="bold"))
self.logo_label.grid(row=0, column=0, padx=20, pady=(20, 10))
self.scaling_label = customtkinter.CTkLabel(self.left_side_panel, text="UI Scaling:", anchor="w")
self.scaling_label.grid(row=7, column=0, padx=20, pady=(10, 0))
self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.left_side_panel, values=["80%", "90%", "100%", "110%", "120%"],
command=self.change_scaling_event)
self.scaling_optionemenu.grid(row=8, column=0, padx=20, pady=(10, 20), sticky = "s")
self.bt_Quit = customtkinter.CTkButton(self.left_side_panel, text="Quit", fg_color= '#EA0000', hover_color = '#B20000', command= self.close_window)
self.bt_Quit.grid(row=9, column=0, padx=20, pady=10)
# button to select correct frame IN self.left_side_panel WIDGET
self.bt_dashboard = customtkinter.CTkButton(self.left_side_panel, text="Dashboard", command=self.dash)
self.bt_dashboard.grid(row=1, column=0, padx=20, pady=10)
self.bt_statement = customtkinter.CTkButton(self.left_side_panel, text="Statement", command=self.statement)
self.bt_statement.grid(row=2, column=0, padx=20, pady=10)
self.bt_categories = customtkinter.CTkButton(self.left_side_panel, text="Manage Categories", command=self.categories)
self.bt_categories.grid(row=3, column=0, padx=20, pady=10)
# right side panel -> have self.right_dashboard inside it
self.right_side_panel = customtkinter.CTkFrame(self.main_container, corner_radius=10, fg_color="#000811")
self.right_side_panel.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True, padx=5, pady=5)
self.right_dashboard = customtkinter.CTkFrame(self.main_container, corner_radius=10, fg_color="#000811")
self.right_dashboard.pack(in_=self.right_side_panel, side=tkinter.TOP, fill=tkinter.BOTH, expand=True, padx=0, pady=0)
# self.right_dashboard ----> dashboard widget
def dash(self):
self.clear_frame()
self.bt_from_frame1 = customtkinter.CTkButton(self.right_dashboard, text="dash", command=lambda:print("test dash") )
self.bt_from_frame1.grid(row=0, column=0, padx=20, pady=(10, 0))
self.bt_from_frame2 = customtkinter.CTkButton(self.right_dashboard, text="dash 1", command=lambda:print("test dash 1" ) )
self.bt_from_frame2.grid(row=1, column=0, padx=20, pady=(10, 0))
# self.right_dashboard ----> statement widget
def statement(self):
self.clear_frame()
self.bt_from_frame3 = customtkinter.CTkButton(self.right_dashboard, text="statement", command=lambda:print("test statement") )
self.bt_from_frame3.grid(row=0, column=0, padx=20, pady=(10, 0))
# self.right_dashboard ----> categories widget
def categories(self):
self.clear_frame()
self.bt_from_frame4 = customtkinter.CTkButton(self.right_dashboard, text="categories", command=lambda:print("test cats") )
self.bt_from_frame4.grid(row=0, column=0, padx=20, pady=(10, 0))
# Change scaling of all widget 80% to 120%
def change_scaling_event(self, new_scaling: str):
new_scaling_float = int(new_scaling.replace("%", "")) / 100
customtkinter.set_widget_scaling(new_scaling_float)
# close the entire window
def close_window(self):
App.destroy(self)
# CLEAR ALL THE WIDGET FROM self.right_dashboard(frame) BEFORE loading the widget of the concerned page
def clear_frame(self):
for widget in self.right_dashboard.winfo_children():
widget.destroy()
a = App()
a.mainloop()

Why is columnspan not recognized? (Tiknter) The size of the button does not change

I was wondering why when I use the columnspan parameter in the .grid() function the button does not change size. Should it not span all columns within the frame? Below I have my code and the output. Thanks all.
# Import Modules
from tkinter import ttk
import tkinter as tk
import os
# Basic Structure
root = tk.Tk()
root.minsize(600, 700)
root.maxsize(600, 700)
root.title("Training HUB")
root.config(bg="slategray")
# Create Frames - Title, Left and Right Frames
title_frame = tk.Frame(root, width=580, height=50, bg='white', highlightbackground="black", highlightthickness=1)
title_frame.grid(row=0, columnspan=2, padx=10, pady=5)
left_frame = tk.Frame(root, width=280, height=550, bg='white', highlightbackground="black", highlightthickness=1)
left_frame.grid(row=1, column=0, padx=10, pady=5)
left_frame.grid_propagate(0)
right_frame = tk.Frame(root, width=280, height=550, bg='white', highlightbackground="black", highlightthickness=1)
right_frame.grid(row=1, column=1, padx=10, pady=5)
right_frame.grid_propagate(0)
# Buttons
button1 = ttk.Button(right_frame, text="Run Processing").grid(
row=0, column=0, columnspan = 2, padx=5, pady=5, sticky = 'ew')
# Run Application
root.mainloop()
Tkinter Output:

Can't remove secondary frame in Python

I'm very new at trying to learn some basic Python and TKinter.
I have written some simple code to create and populate a 2nd frame on a button press but I can't remove the frame when I'm done with it. I can remove the frame if I use command=edit_frame.destroy but I want to do some other cleanup actions as well and so I am trying to use a function for it.
This is my code:
import tkinter as tk
root = tk.Tk()
root.title('Frames test')
root.geometry('490x310')
def edit_end(frame_name,items):
# do other clean up stuff
btn_edit_items.config(state="normal")
items.delete(0, 'end')
try:
frame_name.pack.forget
print('destroyed')
except:
print('something not right')
def edit_items():
btn_edit_items.config(state="disabled")
edit_frame = tk.Frame(root,bg="green")
edit_frame.grid(row=3,rowspan=7, column=2)
edit_frame.tkraise()
ef_items = tk.Listbox(edit_frame)
ef_items.grid(row=2,rowspan=7, column=1, padx=10, pady=2)
ef_items.insert('end', 'Item 2')
btn_remove = tk.Button(edit_frame, text='Remove', bg='white', command=lambda: edit_end(edit_frame,ef_items))
btn_remove.grid(row=0,column=1)
btn_edit_items = tk.Button(root, text='Edit', bg='white', command=edit_items)
btn_edit_items.grid(row=8,column=0, sticky='EW', padx=10, pady=2)
btn_exit = tk.Button(root, text='Exit', bg='white', command=exit)
btn_exit.grid(row=9,column=0)
list_frame = tk.Frame(root, bg="red")
list_frame.grid(row=4,rowspan=7,column=1)
lb_items = tk.Listbox(list_frame)
lb_items.grid(row=2,rowspan=7, column=1, padx=10, pady=2)
lb_items.insert('end', 'Item one')
# Start the main events loop
root.mainloop()
The frame was added using the grid geometry manager, rather than pack:
edit_frame.grid(row=3,rowspan=7, column=2)
...Therefore, the grid_remove() method—or grid_forget(), depending on our needs—must be used to remove it:
try:
frame_name.grid_remove()
print('destroyed')

Python tkinter inserting string in texttbox out of a function

Trying to add text from a function into a textfield, but can't figure out how.
Every time when the Start button is clicked it should add text to the textfield.
import Tkinter
class GuiCreate:
def __init__(self,parent):
#Textbox
window = Frame(width=620, height=50)
window.place(x=25,y=320)
vscroll = Scrollbar(window)
hscroll = Scrollbar(window, orient='horizontal')
# create instance variable with "self"
self.listbox = Text(window, height=10)
self.listbox.pack(side=LEFT, fill=X, padx=5, pady=5, expand=1)
vscroll.config(command=self.listbox.yview, relief=SUNKEN)
hscroll.config(command=self.listbox.xview, relief=SUNKEN)
self.listbox.config(yscrollcommand=vscroll.set, relief=SUNKEN)
self.listbox.config(xscrollcommand=hscroll.set)
f7 = Frame(width=30, height=20)
f7.place(x=20,y=260)
srcButton = Button(f7, text="START", command=self.startProcess)
srcButton.pack(side='left')
def startProcess(self):
textinsert = 'abcdefg'
self.listbox.insert('end', textinsert)
root = Tk()
root.title("Clipfinder")
root.geometry('650x550+200+100')
root.configure(background = 'gray')
gui=GuiCreate(root)
root.mainloop()
Getting the Error: AttributeError: GuiCreate instance has no attribute 'listbox'
How can I send the string out of a function into the textbox?
THX
def __init__(self, parent):
#Textbox
window = Frame(width=620, height=50)
window.place(x=25,y=320)
vscroll = Scrollbar(window)
hscroll = Scrollbar(window, orient='horizontal')
self.listbox = Text(window, height=10)
self.listbox.pack(side=LEFT, fill=X, padx=5, pady=5, expand=1)
vscroll.config(command=self.listbox.yview, relief=SUNKEN)
hscroll.config(command=self.listbox.xview, relief=SUNKEN)
self.listbox.config(yscrollcommand=vscroll.set, relief=SUNKEN)
self.listbox.config(xscrollcommand=hscroll.set)
f7 = Frame(width=30, height=20)
f7.place(x=20,y=260)
srcButton = Button(f7, text="START", command=self.startProcess)
srcButton.pack(side='left')
Forgot to add listbox as an attribute. Otherwise it is just local to the init method..
Try to create the listbox as an instance variable with self:
from Tkinter import *
class GuiCreate:
def __init__(self,parent):
#Textbox
window = Frame(width=620, height=50)
window.place(x=25,y=320)
vscroll = Scrollbar(window)
hscroll = Scrollbar(window, orient='horizontal')
# create instance variable with "self"
self.listbox = Text(window, height=10)
self.listbox.pack(side=LEFT, fill=X, padx=5, pady=5, expand=1)
vscroll.config(command=self.listbox.yview, relief=SUNKEN)
hscroll.config(command=self.listbox.xview, relief=SUNKEN)
self.listbox.config(yscrollcommand=vscroll.set, relief=SUNKEN)
self.listbox.config(xscrollcommand=hscroll.set)
f7 = Frame(width=30, height=20)
f7.place(x=20,y=260)
srcButton = Button(f7, text="START", command=self.startProcess)
srcButton.pack(side='left')
def startProcess(self):
textinsert = 'abcdefg'
self.listbox.insert('end', textinsert)
root = Tk()
root.title("Clipfinder")
root.geometry('650x550+200+100')
gui = GuiCreate(root)
root.configure(background = 'gray')
root.mainloop()
You can learn more about classes and object-orient programming in python here, a few paragraphs down it
touches upon using self.

How to insert a scrollbar and a form in a notebook widget with python and tkinter?

I am really new to python and currently I am trying to design a form using tkinter. I am stuck trying to insert an scrollbar and a form in a notebook since I haven't found an answer to my question, and it is simple "How can I insert a scrollbar and a form in a notebook tkinter widget?"... As you can see is simple for you, but not for a newbie like me!
However, this is what I have done so far, fortunately it does show the scrollbar, but it crashes when I try insert the form into the notebook!
Note: My python version is Python 2.7.3 with EPD_free 7.3-2 (32-bit)
import Tkinter
from Tkinter import *
from ttk import *
import tkMessageBox
import ttk
import Tkinter as tk
root = Tk()
root.title("Model_A")
root.resizable(0,0)
# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
#Child Frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)
ContainerThree = Frame(notebook)
ContainerThree.pack(fill=BOTH, expand=True)
ContainerFour = Tkinter.Frame(notebook)
ContainerFour.pack(fill=BOTH, expand=True)
#Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')
notebook.add(ContainerThree, text='Mode C')
notebook.add(ContainerFour, text='Mode D')
canvas = Canvas(ContainerOne, width=200, height=400)
scroll = Scrollbar(ContainerOne, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerTwo, width=200, height=400)
scroll = Scrollbar(ContainerTwo, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerThree, width=200, height=400)
scroll = Scrollbar(ContainerThree, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerFour, width=200, height=400)
scroll = Scrollbar(ContainerFour, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frame = Frame(canvas, width=200, height=1000)
canvas.create_window(100, 500, window=frame)
frameOne = None
def defocus(event):
event.widget.master.focus_set()
if __name__ == '__main__':
ContainerOne= Tkinter.Label(notebook, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
frameOne.grid(row=2, columnspan=7, sticky='W', \
padx=5, pady=5, ipadx=5, ipady=5)
#Component Selection
componentComb= ttk.Combobox(ContainerOne, width="19")
componentComb = Combobox(ContainerOne, state="readonly", values=("A", "B", "C"))
componentComb.grid(column=4, row=4, columnspan="5", sticky="nswe")
componentComb.set("Main Selection")
root.mainloop()
If you take a look at the options of the Notebook widget, you can see that neither yview nor yscrollcommand are present. Besides, Frame widgets aren't scrollable either.
What you can do is to create a Canvas widget with a Scrollbar inside your frameOne, and then add a Frame to the canvas with create_window.
root = Tk()
root.resizable(0,0)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
container = Frame(notebook)
container.pack(fill=BOTH, expand=True)
notebook.add(container, text='Mode A')
canvas = Canvas(container, width=200, height=400)
scroll = Scrollbar(container, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frame = Frame(canvas, bg='white', width=200, height=1000)
canvas.create_window(100, 500, window=frame)
root.mainloop()
I have solved the problem I had with python and tkinter inserting form into a notebook as well as a scrollbar. I want to thank #A.Rodas for his kind help.
This is my code, hope you find it useful!
import Tkinter
from Tkinter import *
from ttk import *
import tkMessageBox
import math
import ttk
import Tkinter as tk
def defocus(event):
event.widget.master.focus_set()
# start of GUI code
root = Tk()
root.title("Model A")
root.resizable(0,0)
# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
# Child frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)
# Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')
canvas1 = Canvas(ContainerOne, width=1200, height=450)
scroll = Scrollbar(ContainerOne, command=canvas1.yview)
canvas1.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas1.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas2 = Canvas(ContainerTwo, width=1200, height=450)
scroll = Scrollbar(ContainerTwo, command=canvas2.yview)
canvas2.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas2.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frameOne = Frame(canvas1, width=800, height=450)
canvas1.create_window(250, 125, window=frameOne)
frameTwo = Frame(canvas2, width=800, height=450)
canvas2.create_window(200, 140, window=frameTwo)
# Main Frame
#Close Application Button
def quit(root):
root.destroy()
ttk.Button(root, text="Close Application", command=lambda root=root:quit(root)).pack()
if __name__ == '__main__':
#Main Part
stepOne = Tkinter.LabelFrame(frameOne, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
stepOne.grid(row=0, columnspan=5, sticky='nsew', padx=5, pady=5, ipadx=5, ipady=5)
stepTwo = Tkinter.LabelFrame(frameOne, text=" 2. Calculate AP : ", font=("fixedsys", "16","bold italic"))
stepTwo.grid(row=2, columnspan=7, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5)
# First Step Starts
# Component Selection
componentComb= ttk.Combobox(stepOne, width="19")
componentComb = Combobox(stepOne, state="readonly", values=("CDA", "VHS", "DVD"))
componentComb.grid(column=4, row=0, columnspan="5", sticky="nswe")
componentComb.set("Component Selection")
# Temperature Selection
tempComb = ttk.Combobox(stepOne, width="12")
tempComb = Combobox(stepOne, state="readonly", values=("-40", "-30", "-20","-10", "0",))
tempComb.grid(column=0, row=2, columnspan="2", sticky="w")
tempComb.set("Temperature Selection")
# Second Step Starts
inEncLbl = Tkinter.Label(stepTwo, text="Oxide:")
inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)
inEncTxt = Tkinter.Entry(stepTwo, width=6)
inEncTxt.grid(row=2, column=1, sticky='w', pady=2)
outEncLbl = Tkinter.Label(stepTwo, text="Density Rate (DR):")
outEncLbl.grid(row=2, column=5, padx=5, pady=2)
outEncTxt = Tkinter.Entry(stepTwo, width=6)
outEncTxt.grid(row=2, column=7,sticky='w', pady=2)
#End Code
root.mainloop()

Categories