How to show two Frames side by side in Tkinter - python-3.x

This is what I got
This is what I want to get
I've tried it like 2 hours with reading the documentation and every tutorial I could possibly find but after all I'm not able to display two ttk.Frames side by side with a border around them.
What have I done wrong?
This is my code so far (and yeah, I know, I've never used self.path but I just like to have it available in need):
import os
import tkinter as tk
import tkinter.ttk as ttk
from ttkthemes import ThemedTk
class GUI(ThemedTk):
def __init__(self):
super().__init__()
self.set_theme("breeze")
self.path = os.path.dirname(__file__)
self.title("Kniffel All In One")
self.defineVariables()
# Root Window
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=5)
self.columnconfigure(1, weight=1)
self.LeftFrame = ttk.Frame(self, border=True, borderwidth=1)
self.LeftFrame.grid(row=0, column=0, padx=5, pady=5, sticky="NSWE")
self.RightFrame= ttk.Frame(self, border=True, borderwidth=1)
self.RightFrame.grid(row=0, column=1, padx=5, pady=5, sticky="NSWE")
# LeftFrame
self.LeftEntry = ttk.Entry(self.LeftFrame, textvariable=self.text1).grid(row=0, column=0, ipadx=5, ipady=5, sticky="WE")
self.LeftLabel = ttk.Label(self.LeftFrame, text="Little test left side").grid(row=1, column=0, ipadx=5, ipady=5, sticky="WE")
# RightFrame
self.RightEntry = ttk.Entry(self.RightFrame, textvariable=self.text2).grid(row=0, column=0, ipadx=5, ipady=5, sticky="WE")
self.RightLabel = ttk.Label(self.RightFrame, text="Litte test right side").grid(row=1, column=0, ipadx=5, ipady=5, sticky="WE")
self.mainloop()
def defineVariables(self):
self.text1 = tk.StringVar()
self.text2 = tk.StringVar()
if __name__ == '__main__':
GUI()
Note that I didn't use .pack() and .grid() together and it doesn't display a border even though I thought border=True would show one.
Please be so kind to just answer my question since I haven't ask for aesthetics, readability or programming style improvements. Also this won't be a professional application.
Thanks for understanding this.

Related

Why my ttk style won't apply to my Frame?

I've been fighting with ttk style for about 2 days right now, but nothing seems to work. I've tried a lot of different approaches, but I had no effect. Right now, I just want to see that the Frame that I'm trying to add to my App is working, could you please show me where is the problem?
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
# Main window configuration
self.title("Work Tracker")
self.geometry("600x700")
self.resizable(0,0)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
# Styles
style = ttk.Style()
style.configure('TFrame', background='blue')
# Logo frame
frame = ttk.Frame(self, width=200, height=150)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_propagate(False)
frame.grid(row=0, column=0, sticky='nw')
label = ttk.Label(frame, text ="WORK TRACKER")
label.pack()
def main():
app = App()
app.mainloop()
main()

How to create a class to close GUI and exit Python

I have been trying to find a way to do this for a while to no avail. I would like to create a class to completely close my GUI in tkinter and I'm not having much luck. I've tried sys.exit and .destroy() a few different ways. I can manage to do what I want without using classes but I'm rather new to OOP. Here is my code:
import sys as system
import tkinter as tk
from tkinter import ttk
class headerFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
#setup the grid layout manager
self.columnconfigure(0, weight=1)
self._create_widgets()
def _create_widgets(self):
#header bar
canvas = tk.Canvas(self, bg='#0066cc', highlightthickness=0, height=45, width=600)
canvas.grid(column=0, row=0, sticky=tk.W)
label = ttk.Label(self, text='Production Assistant', background='#0066cc', foreground='White', font=('calibri', 18, 'bold'))
label.grid(row=0, column=0)
class loginFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
#setup the grid layout manager
self.columnconfigure(0, weight =1)
self.columnconfigure(0, weight=3)
self._create_widgets()
def _create_widgets(self):
#username
ttk.Label(self, text='Username: ', justify='right').grid(row=0, column=0, sticky=tk.E)
username = ttk.Entry(self, width=33)
username.focus()
username.grid(row=0, column=1, sticky=tk.W)
#password
ttk.Label(self, text='Password: ', justify='right').grid(row=1, column=0, sticky=tk.E)
password = ttk.Entry(self, width=33, show='*')
password.grid(row=1, column=1, sticky=tk.W)
#add padding
for widget in self.winfo_children():
widget.grid(padx=0, pady=5)
class loginButtonFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
#setup the grid layout manager
self.columnconfigure(0, minsize=62)
self._create_widgets()
def _create_widgets(self):
#buttons
ttk.Button(self, text='Login', width=15).grid(row=0, column=1)
ttk.Button(self, text='Forgot Login', width=15).grid(row=0, column=2)
ttk.Button(self, text='Request Access', width=15).grid(row=1, column=1)
ttk.Button(self, text='Exit', width=15, command=exitButton).grid(row=1, column=2)
#add padding to buttons
for widget in self.winfo_children():
widget.grid(padx=3, pady=3)
class exitButton():
def exit():
#code to close gui and program
#create the main application
class mainLogin(tk.Tk):
def __init__(self):
super().__init__()
self.title('Login')
self.geometry('325x175')
self.resizable(0, 0)
self.configure(background='#444444')
#windows only (remove the minimize/maximize buttons)
self.attributes('-toolwindow', True)
#TCL to center the screen
self.eval('tk::PlaceWindow . center')
#layout on the root window
self.columnconfigure(0, weight=1)
self._create_Styles()
self._create_widgets()
def _create_Styles(self):
#create styles
s = ttk.Style()
s.configure('TFrame', background='#444444')
s.configure('TLabel', background='#444444', foreground='white')
s.configure('TButton', background='#878683', foreground='black')
def _create_widgets(self):
#create the header frame
_header_frame = headerFrame(self)
_header_frame.grid(column=0, row=0)
#create the login frame
_login_frame = loginFrame(self)
_login_frame.grid(column=0, row=1, sticky=tk.N)
#create the button frame
_login_button_frame = loginButtonFrame(self)
_login_button_frame.grid(column=0, row=2)
if __name__ == '__main__':
app = mainLogin()
app.mainloop()
class exitButton() is what I would like to call from multiple different pages in the application to close everything.
Any help is appreciated, I'm trying to learn as I build so if you have any suggested reading based around Python that would help with this I would appreciate it!

Ttk Frame, Background color

I'm using ttk, for my GUI. I know that it is also a very simple question ... I am trying to change the background color of the main window.
I tried to change the theme, because I am working on a Mac, (and Python 3.5) to avoid the problem with the theme 'aqua', which is the default.I've been reading about several solutions like these questions which are about the same problem... These are the numbers of the questions:
54476511,
38712352,
47327266,
23750141.
But, I haven't Solve the problem, yet.
Here it's my code.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter.scrolledtext import *
from tkinter import Tk, BOTH, W, N, E, S, messagebox, END
from tkinter.ttk import Button, Label, Style, Frame
class Example(Frame):
def __init__(self,master):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Example")
Style().theme_use("classic")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
self.txt_Pad = ScrolledText(self)
self.txt_Pad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
self.txt_Pad.insert(END,'Type your info here')
btn_save = Button(self, text="Save", command=self.save_command)
btn_save.grid(row=1, column=3)
btn_close = Button(self, text="Close", command=self.onClose)
btn_close.grid(row=2, column=3, pady=4)
btn_help = Button(self, text="Help", command=self.about_command)
btn_help.grid(row=5, column=0, padx=5)
def onClose(self):
self.master.destroy()
def about_command(self):
msb = messagebox.showinfo("About", "\"Insert a useful tip Here\"")
def save_command(self):
print('Your info it\'s save now')
def open_command(self):
print('Choose your File')
def main():
root = Tk()
root.geometry("350x300+300+300")
root.configure(bg='#0059b3')
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
Any Suggestions would be appreciated.
Create a style then apply it.
from tkinter.scrolledtext import *
from tkinter import Tk, BOTH, W, N, E, S, messagebox, END
from tkinter.ttk import Button, Label, Style, Frame
class Example(Frame):
def __init__(self, master):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Example")
# create a new style
self.style = Style()
# configure it to the background you want
self.style.configure('My.TFrame', background='#0059b3')
#Style().theme_use("classic")
# apply it
self.config(style='My.TFrame')
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
self.txt_Pad = ScrolledText(self)
self.txt_Pad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
self.txt_Pad.insert(END,'Type your info here')
btn_save = Button(self, text="Save", command=self.save_command)
btn_save.grid(row=1, column=3)
btn_close = Button(self, text="Close", command=self.onClose)
btn_close.grid(row=2, column=3, pady=4)
btn_help = Button(self, text="Help", command=self.about_command)
btn_help.grid(row=5, column=0, padx=5)
def onClose(self):
self.master.destroy()
def about_command(self):
msb = messagebox.showinfo("About", "\"Insert a useful tip Here\"")
def save_command(self):
print('Your info it\'s save now')
def open_command(self):
print('Choose your File')
def main():
root = Tk()
root.geometry("350x300+300+300")
root.configure(background='#0059b3')
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
I left comments at the parts I changed.

I can't use the text from my Entry widget

There is something in my code that isn't letting me use the text from my Entry widget. I want to use the text a user will enter in my Entry widget, which i've called "textentry", in another part of my code but it doesn't seem to be storing it in a way i can use. In this example i'm just trying to print what is entered into the terminal.
I can get it to print if i uncomment the "print(textentry.get())" in my function.
As it is now i get ".!entry" printed in the terminal. i'm not sure I follow that output either.
I feel like it's probably something simple but i've been struggling a while and many different approaches but still not success.
import tkinter as tk
from tkinter import *
def click():
textentry.get()
# print(textentry.get())
Text_input_window.destroy()
Text_input_window= Tk()
Label (Text_input_window,text="Enter search word:", bg="black", fg="white").grid(row=1, column=0, sticky=W)
textentry = tk.Entry(Text_input_window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=W)
Button(Text_input_window, text="SUBMIT", width=6, command=click).grid(row=3, column=0, sticky=W)
Text_input_window.mainloop()
print(textentry)
Try it:
import tkinter as tk
from tkinter import *
Text_input_window = Tk()
textentry = StringVar()
def click():
global textentry
textentry = textentry_ent.get()
# print(textentry.get())
Text_input_window.destroy()
Label(Text_input_window, text="Enter search word:",
bg="black", fg="white").grid(row=1, column=0, sticky=W)
textentry_ent = tk.Entry(Text_input_window, textvariable=textentry,width=20, bg="white")
textentry_ent.grid(row=2, column=0, sticky=W)
Button(Text_input_window, text="SUBMIT", width=6,
command=click).grid(row=3, column=0, sticky=W)
Text_input_window.mainloop()
print(textentry)

Arranging size of a frame in tkinter

I am making a simple toolbox for my app. I have used the class method, which inherits Frame as its super class. In my main file I import this class.
It will be a main window which all widgets will be in it. But there is a problem, here is the source code:
from tkinter import *
class ToolBox(Frame):
def __init__(self, master=None,
width=100, height=300):
Frame.__init__(self, master,
width=100, height=300)
self.pack()
Button(self, text="B").grid(row=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=0, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=0,sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=1, sticky=(N,E,W,S))
I import this in here:
from tkinter import *
import toolbox as tl
root = Tk()
frame = Frame(root, width=400, height=400)
frame.pack()
tl.ToolBox(frame).pack()
root.mainloop()
Main window, which is the root who has the frame, must be 400 in widht and height. But it appears in dimensions of my toolbox. I want the toolbox to be in the main window. How can I solve this?
You can force the root window to have specific dimensions using the geometry method.
root = Tk()
root.geometry("400x400")
If you would also like the buttons to stretch evenly to fill the whole root window, you need to do two things:
Call rowconfigure and columnconfigure to set the weight of the root and each frame that is a parent of your buttons.
specify the sticky parameter for every button and frame that is a child of your root.
Here's an example. I removed your frame Frame, since it didn't seem to be doing anything. Toolbox is already a frame, after all, and there's not much point putting a frame inside a frame.
from tkinter import *
class ToolBox(Frame):
def __init__(self, master=None,
width=100, height=300):
Frame.__init__(self, master,
width=width, height=height)
for i in range(2):
self.grid_columnconfigure(i, weight=1)
for j in range(3):
self.grid_rowconfigure(j, weight=1)
Button(self, text="B").grid(row=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=0, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=0,sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=1, sticky=(N,E,W,S))
root = Tk()
root.geometry("400x400")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
ToolBox(root).grid(sticky="news")
root.mainloop()
Now your root is properly sized, and your buttons stretch to fill it.

Resources