Add DataFrame to Tkinter's tab's - python

I need to add a Pandas DataFrame to Tkinter one of the tabs. DataFrame is made from csv file.
I have a code
from tkinter import ttk
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.title("Inofin Sprendimai")
root.geometry('500x400')
nb = ttk.Notebook(root)
page1 = ttk.Frame(nb)
page2 = ttk.Frame(nb)
page3 = ttk.Frame(nb)
page4 = ttk.Frame(nb)
nb.add(page1, text='ID And Statistic')
nb.add(page2, text='Table With Names')
nb.add(page3, text="Table With ID's")
nb.add(page4, text='Settings')
nb.pack(fill=BOTH, expand=1)
root.mainloop()`
DataFrame should be in the second and third tab of the table...
I can put DataFrame to Tkinter with this code:
root = Tk()
root.geometry('580x250')
# I put dataframe in here. Now it is just a random df
dates = pd.date_range('20210101', periods=8)
dframe=pd.DataFrame(np.random.randn(8,4),index=dates,columns=list('ABCD'))
txt = Text(root)
txt.pack()
class PrintToTXT(object):
def write(self, s):
txt.insert(END, s)
sys.stdout = PrintToTXT()
print (frame)
mainloop()
Grateful for any help!

Related

Creating popup using Return in pandastable

How can I customize the package panastable so that when i press Return in the display, a warning pops up. I tried to bind Return to the function callback, which should create the messagebox. But nothing happens. It should give the warning after I enter new content in the cell and press Return. This is code I'm using:
import pandas as pd
from pandastable import Table
import tkinter as tk
from tkinter import messagebox
class MyTable(Table):
def callback(self, event):
messagebox.showinfo(title="Achtung", message="Achtung")
def showWarning(self):
self.bind('<Return>', self.callback)
top = tk.Tk()
top.geometry("300x1000")
df = pd.DataFrame()
df["column1"] = [1,2,3,4,5]
frame = tk.Frame(top)
frame.pack(fill="both", expand=True)
pt = MyTable(frame, dataframe=df)
pt.show()
pt.focus_set()
pt.showWarning()
button = tk.Button(top, text="Änderungen speichern", command=top.quit)
button.pack()
top.mainloop()
So there is a much better approach to this IMO (because using what is inherited):
import pandas as pd
from pandastable import Table
import tkinter as tk
from tkinter import messagebox
class MyTable(Table):
#staticmethod
def show_info():
messagebox.showwarning(title="Achtung", message="Achtung")
def handleCellEntry(self, row, col):
super().handleCellEntry(row, col)
self.show_info()
root = tk.Tk()
root.geometry("300x1000")
df = pd.DataFrame()
df["column1"] = [1, 2, 3, 4, 6]
frame = tk.Frame(root)
frame.pack(fill="both", expand=True)
pt = MyTable(frame, dataframe=df)
pt.show()
button = tk.Button(root, text="Änderungen speichern", command=root.quit)
button.pack()
root.mainloop()
Using the inherited method handleCellEntry which is called when enter key is pressed while editing the entry, just what you seem to need. You can see in the source code how this method is bound to '<Return>' sequence to that specific entry (so just override it, call the super method (so it does what it is supposed to do) and then your own method (or vice versa but then the entry will be changed only after you close the dialog)).
The solution was binding it the top window. The following code get the wanted result:
import pandas as pd
from pandastable import Table
import tkinter as tk
from tkinter import messagebox
top = tk.Tk()
top.geometry("300x1000")
df = pd.DataFrame()
df["column1"] = [1,2,3,4,5]
frame = tk.Frame(top)
frame.pack(fill="both", expand=True)
def callback(*args):
messagebox.showinfo(title="Achtung", message="Achtung")
top.bind('<Return>', callback)
pt = Table(frame, dataframe=df)
pt.show()
button = tk.Button(top, text="Änderungen speichern", command=top.quit)
button.pack()
top.mainloop()

How to expand entry box with window in tkinter?

I'm just creating simple window using tkinter which have entry box and search button. What is want is when i maximize window search bar also starch but it is not happening,
Here is my code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side = "top",fill = "both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame,width = 40,textvariable = search_var)
search_bar.grid(row = 0,column = 0)
search_button = ttk.Button(search_frame,text = "Search")
search_button.grid(row = 1,column = 0)
boot = first()
boot.labels()
root.mainloop()
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side="top", fill="both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame, width=20, textvariable=search_var)
search_bar.pack(fill="x")
search_button = ttk.Button(search_frame, text="Search")
search_button.pack()
boot = first()
boot.labels()
root.mainloop()
Not sure this is what you're looking for but try it out.
Here I have used fill='x' in the .pack() geometry manager to fill the row

How to enable an element again after a process has been completed in Tkinter

So I have a notebook with 2 pages. Initially tab1 is disable. Clicking a button in tab0 triggers an external process of creating a file. Only once this file is created should tab1 be enabled again.I can't edit the code that creates this file and creating it take some time so in the meantime my code should constantly check if it is created and then enable the tab... How can I do this is tkinter?
from tkinter import *
from tkinter import ttk
root = Tk()
note = ttk.Notebook(root)
tab0 = ttk.Frame(note)
tab1 = ttk.Frame(note)
note.add(tab0)
note.add(tab1, state="disabled") # tab disabled
note.pack(expand = 1, fill = "both")
# <----------------tab0------------------>
canvas_home = Canvas(tab0,bg='#398FFF')
canvas_home.pack(expand=True,fill=BOTH)
# In my case create_file is being imported...
def create_file():
import time
time.sleep(100) # assume a file is being created
button = Button(canvas_home, text="Create file",command=create_file)
button.grid()
# <----------------tab1------------------>
import os
if os.path.exists('filename'):
note.tab(0, state="normal") # tab enabled
if __name__ == "__main__":
root.mainloop()
Some mistakes:You didn't put your button in your Frame.You want to enable tab1 but you enable tab0
Assume you want to create a txt file called create_text,You could use .after() to check the file whether exist.
In this example,it will check the file per 0.1 second:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import os
def create_file():
create_path = filedialog.asksaveasfilename()
if create_path:
with open(create_path, "w") as f: # create a empty text
pass
def check_file():
if os.path.exists('create_text.txt'): # check whether it exists
note.tab(1, state="normal")
else:
root.after(100, check_file)
root = Tk()
note = ttk.Notebook(root)
tab0 = ttk.Frame(note)
tab1 = ttk.Frame(note)
note.add(tab0)
note.add(tab1, state="disabled") # tab disabled
note.pack(expand=1, fill="both")
# <----------------tab0------------------>
canvas_home = Canvas(tab0, bg='#398FFF')
canvas_home.pack(expand=True, fill=BOTH)
button = Button(canvas_home, text="Create file", command=create_file)
button.grid()
root.after(100, check_file)
if __name__ == "__main__":
root.mainloop()

Tkinter Listbox and Scrollbar not displaying

I am having this issue where the scroll bar is not displaying on the listbox. I do not know what the issue is as.
I believe the issue is originating from the Scrollbar variables as the Listbox appears to be displaying and functioning properly.
The output is displaying the listbox with no scrollbar on the right (as set)
Here is the Listbox with the for loop however, it is displaying the wrong dimensions
Here is the code:
#imports
from tkinter import *
from tkinter import messagebox as ms
from tkinter import ttk
import sqlite3
from PIL import Image,ImageTk
import datetime
global time
time = datetime.datetime.now()
class main:
def __init__(self,master):
self.master = master
def search_user_sql(self):
self.search_user_sqlf = Frame(self.master, height=300, width =200)
scrollbar = Scrollbar(self.search_user_sqlf)
scrollbar.pack(side = RIGHT,fill = BOTH)
myList = Listbox(self.search_user_sqlf, yscrollcommand= scrollbar.set)
myList.pack( side = LEFT, fill = BOTH, expand = 2)
scrollbar.config( command = myList.yview )
self.search_user_sql()
root = Tk()
root.title("Gym Membership System")
main(root)
root.mainloop()
You need to pack the frame to display it. To pack() the frame with the correct size settings, try:
search_user_sqlf = Frame(master, height=300, width=200)
search_user_sqlf.pack(expand=True, fill='both')
search_user_sqlf.pack_propagate(0)
Here is how to attach a scrollbar to list set in a frame in Tkinter:
from tkinter import *
master = Tk()
search_user_sqlf = Frame( master, width=400, height=400)
search_user_sqlf.pack(expand=True, fill='both')
search_user_sqlf.pack_propagate(0)
scrollbar = Scrollbar(search_user_sqlf)
scrollbar.pack(side=RIGHT, fill=Y)
myList = Listbox(search_user_sqlf, yscrollcommand=scrollbar.set)
for line in range(100):
myList.insert(END, "This is line number " + str(line))
myList.pack( side = LEFT, fill = BOTH , expand = 2)
scrollbar.config( command = myList.yview )
mainloop()

How do i use grid system so that a text box does not change column size

What i mean.jpg
(The Image above explains my problem)
My code:
import tkinter
from tkinter import *
import os
from tkinter import ttk
from tkinter.ttk import *
window = tkinter.Tk()
text = Text(window)
def save():
print("Save")
def load():
print("Load")
def printcom():
print("Print")
savebut = ttk.Button(window, text = "Save", command = save)
loadbut = ttk.Button(window, text = "Load", command = load)
printbut = ttk.Button(window, text = "Print", command = printcom)
savebut.grid(row=0,column=0,columnspan=1)
loadbut.grid(row=0,column=1, columnspan=1)
printbut.grid(row=0,column=2, columnspan=1)
text.grid(row=1,column=3, columnspan = 3)
I don't know how to get the text box to not change the column size

Categories