How to keep widgets when a new tab(ttk notebook) is created? - python-3.x

So I have a starter tab which has a textbox inside. I also want to create a new tab on the click of a button. When I create the new tab it does not show the textbox. I want all the tabs to have the same textbox and the same widgets.
Here is my code so far:
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("600x600")
def newTab(*args): # How do I keep the textbox and other widgets in the new tabs
newFrame = Frame(root, width=500, height=500)
newFrame.pack()
tabsArea.add(newFrame, text="Untitled.txt")
button = Button(root, command=newTab).pack()
# Tab Area --- First Tab
tabsArea = ttk.Notebook(root)
tabsArea.pack(pady=15)
# Create Main Frame
frame = Frame(root)
frame.pack(pady=5)
# Add Frame to Tab
tabsArea.add(frame, text="Untitled.txt")
# Textbox
textBox = Text(frame).pack()
root.mainloop()
How do I configure the text box and widgets in the newFrame/new tab?

You need to add a new text widget in each tab. Your code is just creating empty frames. Also, you shouldn't be calling newFrame.pack() since you are adding newFrame to the notebook.
Here is a slimmed down version of your code that shows the basic concept:
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("600x600")
def newTab(*args):
newFrame = Frame(root, width=500, height=500)
tabsArea.add(newFrame, text="Untitled.txt")
text = Text(newFrame)
text.pack(fill="both", expand=True)
button = Button(root, text="New Tab", command=newTab)
tabsArea = ttk.Notebook(root)
button.pack(side="top")
tabsArea.pack(pady=15, fill="both", expand=True)
# Add the first tab
newTab()
root.mainloop()

Related

Create notepad via python

Scope - Python3.10
Looking for -
add multiple editable notes in tabs. (just like notepad)
How to add a save function for a specific note
enter image description here
Attaching work done till now, Help is much appreciated
from tkinter import *
from tkinter import ttk
MainWindow = Tk() #Object#
MainWindow.geometry ('380x250') #size of object#
MainWindow.title("Bnote") #Name of object#
Menubar = Menu(MainWindow)
Appearance = Menu(Menubar,tearoff=0)
NotesArea = Text(MainWindow)
def test(Menubar):
Menubar.entryconfigure(1, label="click")
file_menu = Menu(Menubar, tearoff=False)
file_menu.add_command(label="New_Tab", command=lambda: clicked(file_menu))
Menubar.add_cascade(label="+", menu=file_menu)
scrollbar = Scrollbar(MainWindow) #adding scrollbar#
scrollbar.pack(side=RIGHT,fill=Y) #packing scrollbar#
MainWindow.config(menu=Menubar)
NotesArea = Text(MainWindow,yscrollcommand=scrollbar.set,wrap = WORD, font="Ubuntu 12")
scrollbar.config(command=NotesArea.yview) # configuring scrollbar
NotesArea.pack(fill=BOTH)
MainWindow.mainloop ()

Tkinter Toplevel window not appearing

Python 3.8, Win 10 is the os, Toplevel widget does not appear to be working with new window not appearing. Any guidance would be appreciated, thanks!
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
top.mainloop()
my_button = Button(root, text="Popup, click here", command="popup")
my_button.grid(row=0, column=0)
root.mainloop()
Problem:
The only issue here is that the callback command shouldn't be a string.
Solution:
Remove the quotes around popup and the Toplevel window should appear.
Fixed Code:
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
my_label_top.pack()
my_button = Button(root, text="Popup, click here", command=popup)
my_button.grid(row=0, column=0)
root.mainloop()
Tips:
Using top.mainloop() is not necessary.
You also forgot to pack() the Label(my_label_top)

Python tkinter how can I copy information from a Text widget to a Canvas widget?

To put it simple, i want to write stuff in the text widget and then have it copied onto a canvas widget on the same coordinates (in the code below the Text widget size is the same as the canvas one)
here is an image of the code
Here is the written code:
from tkinter import *
#root configuration
root = Tk()
root.config(bg='black')
#function that "replaces" text with canvas
def replace_TextWidget_with_CanvasWidget():
global text_editor
global written_text
#gets text from the text widget and destroys it
written_text = text_editor.get('0.0', '100.0')
text_editor.destroy()
#i want the copied text from the text widget to be inserted on the canvas
text_editor = Canvas(root, height=851, width=601, bg='white')
text_editor.pack()
text_editor.insert()
#button
button = Button(root, command=replace_TextWidget_with_CanvasWidget)
button.pack()
#initial text widget
text_editor = Text(root, height=50, width=75)
text_editor.pack()
text_editor.insert('1.0', 'HERE I WRITE SOMETHING I WANT TO BE COPIED ON THE CANVAS LATER')
root.mainloop()
It looks like you're trying to show some text to display only. If that's the case, set it's state property to disabled after updating it.
text_editor.configure(state='disabled')
However, if you really want to replace it with a canvas with the same information, here is your modified code. Please note that canvas widget doesn't have any insert method. Instead, it has a create_text(x,y,text='my text') method.
from tkinter import * #Not a good programming approach, use import tkinter as tk instead
#function that "replaces" text with canvas
def replace_TextWidget_with_CanvasWidget():
global text_editor
#gets text from the text widget and destroys it
written_text = text_editor.get('0.0', '100.0')
text_editor.destroy()
#i want the copied text from the text widget to be inserted on the canvas
new_canvas = Canvas(root, height=200, width=400, bg='white')
new_canvas.pack()
root.update() #Necessary if you want to get the current coordinates of the canvas
# print(new_canvas.winfo_x(),new_canvas.winfo_y()) #print canvas x,y pos
new_canvas.create_text(new_canvas.winfo_x()+200,new_canvas.winfo_y(), text=written_text) #Create text at x,y coords
#root configuration
root = Tk()
root.config(bg='black')
#button
button = Button(root, width=10, height=2, text="OK", command=replace_TextWidget_with_CanvasWidget)
button.pack(padx=10, pady=10)
#initial text widget
text_editor = Text(root, height=10, width=50)
text_editor.pack()
text_editor.insert('1.0', 'HERE I WRITE SOMETHING I WANT TO BE COPIED ON THE CANVAS LATER')
root.mainloop()
You may have trouble positioning the text at the correct x,y co-ordinates and with text wrapping. If you want text wrapping, create_text has a width property. You can specify it to a specific dimension like width=100
😉
To add text to Canvas
text_id = text_editor.create_text(0, 0, text=written_text, anchor='nw')
# from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
# --- functions ---
def replace_TextWidget_with_CanvasWidget():
global text_editor
global written_text
written_text = text_editor.get('1.0', 'end')
text_editor.destroy()
text_editor = tk.Canvas(root, height=851, width=601, bg='white')
text_editor.pack()
text_id = text_editor.create_text(0, 0, text=written_text, anchor='nw')
#text_editor.insert(text_id, 'end', written_text)
# --- main ---
root = tk.Tk()
root.config(bg='black')
button = tk.Button(root, text="OK", command=replace_TextWidget_with_CanvasWidget)
button.pack()
text_editor = tk.Text(root, height=50, width=75)
text_editor.pack()
text_editor.insert('1.0', 'HERE I WRITE SOMETHING I WANT TO BE COPIED ON THE CANVAS LATER')
root.mainloop()

How to get these tkinter scrollbars working?

Hi cant get these scrollbars working even though this code comes from a very advanced user on this site I have tried everything. No errors they just dont show up
import tkinter as tk
#Make Window
root = tk.Tk()
root.geometry("612x417")
root.title("Exchange Rates")
root.resizable(0,0)
root.configure(background='lightgrey')
#End
#Create listboxes for currency selection
listbox1 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
listbox2 = tk.Listbox(root, font="Helvetica 11 bold", height=3, width=10)
#Try to create a scroll bar
scrollbar1 = tk.Scrollbar(root, orient="vertical", command=listbox1.yview)
listbox1.configure(yscrollcommand=scrollbar1.set)
scrollbar2 = tk.Scrollbar(root, orient="vertical", command=listbox2.yview)
listbox2.configure(yscrollcommand=scrollbar2.set)
listbox1.place(x=300,y=50)
listbox2.place(x=300,y=125)
scrollbar3 = Scrollbar(root)
scrollbar3.pack(side="right", fill="y")
listbox = Listbox(root, yscrollcommand=scrollbar3.set)
listbox.pack()
scrollbar3.config(command=listbox.yview)
root.mainloop()
I don't know how you managed to run it without an error because you imported tkinter as tk but for listbox you put Listbox (not tk.Listbox) or for scrollbar3 you put Scrollbar (not tk.Scrollbar). Also they don't show up because you haven't packed/placed them! And... you have to use either place, pack or grid you can't use them together. You used .place() for your listbox1 and 2 but then you used .pack() for your scrollbar3 and listbox. Whatever you use first (here it's place) will work but the others just simply won't show up.
The below script should hopefully show you how Scrollbars work in a clear and concise way and how they are affected by the number of items entered into a listbox
from tkinter import *
class App:
def __init__(self, master):
self.master = master
self.top = Toplevel(self.master)
self.frame = Frame(self.top)
self.entry = Entry(self.master)
self.button = Button(self.master, text="Ok", command=self.command)
self.entry.pack()
self.button.pack()
self.top.withdraw()
self.frame.pack()
def command(self):
self.frame.destroy()
self.frame = Frame(self.top)
self.listbox = Listbox(self.frame)
self.scroll = Scrollbar(self.frame, orient="vertical", command=self.listbox.yview)
self.listbox.configure(yscrollcommand=self.scroll.set)
for i in range(int(self.entry.get())):
self.listbox.insert(END, "Col"+str(i))
self.frame.pack()
self.listbox.pack(side="left")
self.scroll.pack(side="left", expand=True, fill=Y)
self.top.deiconify()
root = Tk()
app = App(root)
root.mainloop()
Also please take into account that in order for us to review a problem and help you work through it, we need to be able to run and review the code in an easy and digestible fashion.
Hello people answering my own question, here is a working Listbox within a Frame with a Scrollbar.
import tkinter as tk
#Make Window
root = tk.Tk()
root.geometry("612x417")
root.title("Exchange Rates")
root.resizable(0,0)
root.configure(background='lightgrey')
#End
#Try to create a listbox with a scroll bar within a frame
#Create elements
frame = tk.Frame(root, bd=1, relief='sunken', width=150, height=300)
scrollbar = tk.Scrollbar(frame)
listbox = tk.Listbox(frame)
#Attach listbox to scrollbar
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
#Poulate listbox
for i in range(100):
listbox.insert('end', i)
#Pack elements
frame.pack(side='top')
scrollbar.pack(side='right', fill='y')
listbox.pack()
root.mainloop()

okay so I created this code to create a GUI but i need to add buttons and when ever i try it creates a new window. what should I do?

this is my code so far o cant add buttons with out it creating more windows
////////
#import tkinter
import tkinter
#import tkmessagebox(buttons)
from tkinter import *
#create a new window
window = tkinter.Tk()
#title <------ put it before .mainloop
window.title("yeahh boiiii")
#window size
window.geometry("500x500")
#set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico')#<---- 8bit file name
master = Tk()
def callback():
print ("click!")
b = Button(master, text="OK", command=callback)
b.pack()
#draws the window
window.mainloop()
////////
please help
Your problem is that you create 2 instances of Tk(). This is a bad idea, and you don't need to do it since you can make your button a child of the window object:
# Import tkinter
import tkinter as tk
# Create a new window
window = tk.Tk()
# Title <------ put it before .mainloop
window.title("yeahh boiiii")
# Window size
window.geometry("500x500")
# Set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico') #<---- 8bit file name
def callback():
print ("click!")
b = tk.Button(window, text="OK", command=callback)
b.pack()
# Draw the window
window.mainloop()
I also rewrote your tkinter import, because you were importing it twice...

Resources