Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am attempting to nest one ttk notebook within another so that I can have multiple tab levels.
Imagine an upper notebook with a tab for each food group, and within each of those foodgroup tabs, a tab for examples of foods in that group. A tabbed hierarchy.
Is it possible with ttk notebooks? I have not been able to find any reference or examples that deal with this question.
It seems like this code should work. I get no errors, but I can't see the second level tabs. Any help would be appreciated.
#import tkinter and ttk modules
from tkinter import *
from tkinter import ttk
#Make the root widget
root = Tk()
#Make the first notebook
nb1 = ttk.Notebook(root)
nb1.pack()
f0 = Frame(nb1)
f0.pack(expand=1, fill='both')
###Make the second notebook
nb2 = ttk.Notebook(f0)
nb2.pack()
#Make 1st tab
f1 = Frame(nb1)
#Add the tab to notebook 1
nb1.add(f1, text="First tab")
#Make 2nd tab
f2 = Frame(nb1)
#Add 2nd tab to notebook 1
nb1.add(f2, text="Second tab")
###Make 3rd tab
f3 = Frame(nb2)
#Add 3rd tab to notebook 2
nb2.add(f3, text="First tab")
###Make 4th tab
f4 = Frame(nb2)
#Add 4th tab to notebook 2
nb2.add(f4, text="Second tab")
root.mainloop()
Solved:
Here is the working code simplified with notation. Hopefully others will find it instructive. This example uses the model of College Program>Terms>Courses
#import tkinter and ttk modules
from tkinter import *
from tkinter import ttk
#Make the root widget
root = Tk()
#Make the first notebook
program = ttk.Notebook(root) #Create the program notebook
program.pack()
#Make the terms frames for the program notebook
for r in range(1,4):
termName = 'Term'+str(r) #concatenate term name(will come from dict)
term = Frame(program) #create frame widget to go in program nb
program.add(term, text=termName)# add the newly created frame widget to the program notebook
nbName=termName+'courses'#concatenate notebook name for each iter
nbName = ttk.Notebook(term)#Create the notebooks to go in each of the terms frames
nbName.pack()#pack the notebook
for a in range (1,6):
courseName = termName+"Course"+str(a)#concatenate coursename(will come from dict)
course = Frame(nbName) #Create a course frame for the newly created term frame for each iter
nbName.add(course, text=courseName)#add the course frame to the new notebook
root.mainloop()
Related
I have made a tkinter based windows application that in which 2 window get created which show some simple global variable that are displayed in both window and can be changed by entering value in any window too, everything is working fine only problem is taskbar label comes grouped together in windows taskbar which makes it difficult to navigate between two windows, it there any way both windows appear separately in windows taskbar.( NOT by using windows inbuilt option to make all taskbar labels separate)
this is my code it's just a basic code to show my problem.
from tkinter import ttk
from tkinter import *
root = Tk()
root.geometry("200x200")
root.title("root win 1")
Label(root, text='this is first window').pack()
def extra_root():
new_root = Toplevel(root)
new_root.title("root win 2")
new_root.geometry("200x200")
Button(new_root,text='this is 2nd window' , command=None).pack()
new_root.mainloop()
#to create 2nd window
Button(root, command=extra_root).pack()
root.mainloop()
this is my how it comes currently
this is how I want it to appear (this is achieved by using windows inbuilt feature to make all taskbar label to appear separately)
I'm Bloodly Beginner in Python. I was learning to create tabs in python using tkinter pkg. I have successfully created 2 tabs, but I can't show Text (Label) in both tab. When I show only 1 text in one tab (heading.pack()) it works perfectly. But when I show another text in Tab 2 (heading2.pack()), The frame Resulation Broked. I have also set width and height in Frame Option. I want to fix resulation(Before adding 2nd text resulation). Here is Before and after Screenshots
Before adding 2nd Text
After adding 2nd Text
Here is The Code:
from tkinter import *
from tkinter import ttk
root=Tk()
root.title('Tab Widget')
root.geometry('600x400')
book=ttk.Notebook(root)
book.pack(pady=2)
tab1=Frame(book,width=600,height=500,bg='white')
tab2=Frame(book,width=600,height=500,bg='white')
tab1.pack(fill='both',expand=1)
tab2.pack(fill='both',expand=1)
book.add(tab1,text='Tab 1')
book.add(tab2,text='Tab 2')
heading=Label(tab1,text='This is Tab 1',font='arial 20 bold',bg='white')
heading2=Label(tab2,text='This is Tab 2',font='arial 20 bold',bg='white')
heading.pack() #Show Text in Tab 1
heading2.pack() #Show Text in Tab 2
root.mainloop()
When you call heading.pack(), tab1 will be shrink to the size of the label. And if you don't call heading2.pack(), tab2 will still have size 600x500. So the notebook client area will be kept at 600x500.
However when you call heading2.pack(), tab2 will be shrink to the size of the label as well which cause the notebook client area to be shrink as well.
You can avoid the above size adjustment by changing:
book.pack(pady=2)
to
book.pack(pady=2, fill='both', expand=1)
so that notebook will use all the available space of the root window.
Also note that the following two lines are not necessary and can be removed:
tab1.pack(fill='both',expand=1)
tab2.pack(fill='both',expand=1)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is my current db.py file, it contains the tkinter code for creating the GUI:
import tkinter
import db
app = Tk()
app.geometry("450x300")
app.mainloop()
You can use a Entry widget with a linked variable to get the input from user. The content can then be retrieved from the variable and written to a file using file objects.
I like to use themed-tkinter(ttk). If you just starting with creating GUI, I suggest you read more about themed-tkinter here.
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
# StringVar has all the logic to store and update string values
content = tk.StringVar()
def callback():
# This function is invoked when button `submit` is clicked
with open('content.txt', 'w') as file:
file.write(content.get())
entry = ttk.Entry(root, textvariable=content).grid()
submit = ttk.Button(root, text='submit', command=callback).grid()
root.mainloop()
Edit: I worded my answer wrongly for which I appologize. Tkinter by itself is indeed robust and powerful. I found it more easier to use ttk in many cases and had a softcorner towards it.
So I have the following code that creates the GUI in the picture below:
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
# Some code
# Creating Master TreeView
treeView = ttk.Treeview(root)
treeView.heading("#0", text="Variables", anchor=tk.W)
treeView.place(relx=0, rely=0.00,relwidth=0.1,relheight=1)
# Some Code
# Creating Folders/Sub Folders
var = treeView.insert("", 0, text=name)
treeView.insert(var, "end", text="Type: "+type)
treeView.insert(var, "end", text="Value: "+str(value))
This is what It looks like without being pressed and then pressed
Is there anyway to decrease the tabspace of the sub folders? Like bring it back to where the black point is?
For context, this is what the whole gui looks like:
I have to reserve so much space for the Treeview just to make sure the subfolders appear on the screean, and it takes up way to much space. I tend to find that the treeview uses a lot of unnecessary space when adding subfolders
You can try and constain the tree column:
treeView.column('#0', width=your_width, stretch=False)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to make some basic GUI and I'm having some trouble with this code:
with open(project_dir + 'logs/wash.log') as f:
for line in f.readlines():
if not line.startswith('BSSID ') and \
not line.startswith('------------'):
print(line)
At this point of the application I have the tkinter root already opened in the background and a terminal opened too, I would like to open a new tkinter window in which display the line printed above, I suppose adding a label in the window for each line that I need to display.
I tried tk.Toplevel() but I don't know how to make the new window in which display the strings.
My problem is I am trying to create a new window and print the strings there, I have tried tk.Toplevel() but I don't know how to make the new window in which to display the strings
From your comment:
my problem is to create a new window and print there the strings, I tried tk.Toplevel() but I don't know how to make the new window in which display the strings
This is a simple example but should help.
I have a button on the root window that links to a function called new_window(). This function will create a top level window containing a text box widget. We then use the with open statement to write the data to the text box.
import tkinter as tk
root = tk.Tk()
def new_window():
top = tk.Toplevel(root)
my_text_box = tk.Text(top)
my_text_box.pack()
with open(project_dir + 'logs/wash.log') as f:
for line in f.readlines():
if not line.startswith('BSSID ') and \
not line.startswith('------------'):
my_text_box.insert("end", line)
open_new_window = tk.Button(root, text="Open Toplevel", command=new_window)
open_new_window.pack()
root.mainloop()