tkinter Text and Radiobutton widgets - python

I have this small python tkinter project that basically does two things:
It receives data from the tkinter text widget and enters it into the tkinter Radiobutton widget in another window. Everything is working just fine except one issue.
It treats everything I enter as one and create only one Radiobutton. I want it to create a new Radiobutton anytime I move to a new line
Here is my code:
from tkinter import*
import re
import tkinter.scrolledtext as src
root=Tk()
root.geometry("400x400")
root.title("Still trying hard!!")
opt=StringVar()
db=opt.get()
var=StringVar()
var.get()
Txt_Cont=""
cont_formater=""
#FUNCTIONS
def _Text_Input(a):
if isinstance(db,str):
global Txt_Cont
root1 = Toplevel()
root1.geometry("400x400+500+200")
root1.title("DB AND TB WINDOWS")
cont=Txt_Cont
Radiobtn=Checkbutton(root1,text=cont, variable=var)
Radiobtn.deselect()
Radiobtn.pack()
root1.mainloop()
def btnfcn():
global Txt_Cont
Txt_Cont = text_box.get("1.0", "end-1c")
text_box = src.ScrolledText(root, width=40, height=10, bd=10, font=('arial', 10, 'bold'), padx=5,
pady=5)
text_box.pack()
btn = Button(root, text="click me", font=('arial', 10, 'bold'), command=btnfcn)
btn.pack(ipadx=50, pady=(10, 10))
Options = OptionMenu(root, opt, "Databases", "Tables", command=_Text_Input)
Options.pack(ipadx=60)
root.mainloop()

Related

Display text live on Text widget while typing on another Text as input

Hi I am a beginner to Python. How to display text live in tkinter Text widget while i am typing on another Text or Entry widget? Can i use root.after to keep getting the contents?
from tkinter import *
root = Tk()
root.geometry('600x400')
def display():
tDisplay.config(text=tEnter.get(1.0, END))
root.after(1000, display)
tDisplay = Text(root, height=2, width=20, padx=10, pady=10)
tDisplay.grid(row=0, column=0, padx=5, pady=5)
tEnter = Text(root, height=2, width=20, padx=10, pady=10)
tEnter.grid(row=1, column=0, padx=5, pady=5)
display()
root.mainloop()
Yes you can also use after() to get the contents, at the same time you should also delete the current item inside the text box so as to not keep adding to the textbox:
def display():
tDisplay.delete('1.0','end')
tDisplay.config(text=tEnter.get('1.0', 'end-1c'))
root.after(1000, display)

Append combobox values from textbox using a button

Using Python I have created a frame with empty ComboBox, TextBox and a Button. My ComboBox values are taken from List acitivity_list which initialy is empty. I am looking for a solution where user can insert a text into a TextBox and append the List activity_list so it appears in ComboBox by clicking the Button.
I failed implementing the append() function to update the List. My goal is to have a functionality where I write the name of activity in the TextBox, click the 'Add' Button and it appears in my ComboBox.
Thank you.
from tkinter import *
from tkinter import ttk
class Activity:
def __init__(self,root):
self.root=root
self.root.title("database")
self.root.geometry("1350x700+0+0")
title=Label(self.root, text="Daily Activities Database", font=("Calibri",40,"bold"))
title.pack(side=TOP)
#ComboBox
activity_list = []
Frame1=Frame(self.root,bd=4, relief=RIDGE)
Frame1.place(x=20, y=75, width=355, height=560 )
combo_activity=ttk.Combobox(Frame1, font=("Calibri",20))
combo_activity["values"]= activity_list
combo_activity.grid(row=10, column=1, padx=20, pady=10)
#Textbox
txt_act=Entry(Frame1, font=("Calibir",20))
txt_act.grid(row=11, column=1, padx=20, pady=20)
#Button
bt1 = ttk.Button(Frame1, text = "Add")
bt1.grid(row=12, column=1, padx=20, pady=20)
root=Tk()
ob=Activity(root)
root.mainloop()
Add an instance method which is triggered when Add is clicked. In the function, add the user input into activity_list and then update the values option of combo_activity.
However, you need to change some local variables to instance variables, otherwise they cannot be accessed inside the new function:
from tkinter import *
from tkinter import ttk
class Activity:
def __init__(self, root):
self.root = root
self.root.title("database")
self.root.geometry("1350x700+0+0")
title=Label(self.root, text="Daily Activities Database", font=("Calibri",40,"bold"))
title.pack(side=TOP)
Frame1 = Frame(self.root,bd=4, relief=RIDGE)
Frame1.place(x=20, y=75, width=355, height=560 )
#ComboBox
self.activity_list = []
self.combo_activity = ttk.Combobox(Frame1, font=("Calibri",20))
self.combo_activity["values"] = self.activity_list
self.combo_activity.grid(row=10, column=1, padx=20, pady=10)
#Textbox
self.txt_act = Entry(Frame1, font=("Calibir",20))
self.txt_act.grid(row=11, column=1, padx=20, pady=20)
#Button
bt1 = ttk.Button(Frame1, text="Add", command=self.add_activity) # added command option
bt1.grid(row=12, column=1, padx=20, pady=20)
def add_activity(self):
activity = self.txt_act.get().strip()
if activity:
self.activity_list.append(activity)
self.combo_activity["values"] = self.activity_list
root = Tk()
ob = Activity(root)
root.mainloop()
Thank you for help. I have adjusted the code like this and the button disappeared for the frame. Perhaps there is something wrong with indentation?
from tkinter import *
from tkinter import ttk
class Activity:
def __init__(self,root):
self.root=root
self.root.title("database")
self.root.geometry("1350x700+0+0")
title=Label(self.root, text="Daily Activities Database", font=("Calibri",40,"bold"))
title.pack(side=TOP)
#ComboBox
activity_list = []
Frame1=Frame(self.root,bd=4, relief=RIDGE)
Frame1.place(x=20, y=75, width=355, height=560 )
combo_activity=ttk.Combobox(Frame1, font=("Calibri",20))
combo_activity["values"]= activity_list
combo_activity.grid(row=10, column=1, padx=20, pady=10)
#Textbox
txt_act=Entry(Frame1, font=("Calibir",20))
txt_act.grid(row=11, column=1, padx=20, pady=20)
#Button
def add_task(self):
activity = txt_act.get()
activity_list.append(activity)
return activity_list
bt1 = ttk.Button(Frame1, text = "Add")
bt1.grid(row=12, column=1, padx=20, pady=20, command=add_task)
root=Tk()
ob=Activity(root)
root.mainloop()

Tkinter StringVar shows random numbers instead of variable on label

The title says it all.
import tkinter as tk
from tkinter import filedialog, Text, StringVar
root = tk.Tk()
root.resizable(False, False)
var1 = StringVar()
topn = ""
print(topn)
def addApp():
global topn
topn = topn + "1"
print(topn)
var1.set(topn)
canvas = tk.Canvas(root, height=500, width=400, bg="#263D42")
canvas.pack()
frame = tk.Frame(root, bg="orange")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
Resultlabel = tk.Label(root,text=var1.get, padx=100, pady=5, fg="white", bg="grey")
Resultlabel.pack()
t
openFile = tk.Button(frame, text="Open File", padx=10, pady=5, fg="white", bg="#263D42", command=addApp)
openFile.pack()
root.mainloop()
[btw, ignore the Open File, i was following a tutorial to learn the basics of tkinter and then edited the code to fit my needs. I forgot to change the text in the button.]
https://i.stack.imgur.com/2FVKG.png
text is not the same thing as textvariable. text is used to associate static text with a widget, textvariable is used to have the text of the widget be synchronized with the value of a variable.
If you want ResultLabel to always show what is in var1 you must define it like this:
Resultlabel = tk.Label(root,textvariable=var1, padx=100, pady=5, fg="white", bg="grey")

How do I add a Scrollbar to a tkinter Text Widget using .grid method

I am trying to create a Scrollbar for my text widget however, I cannot seem to be able to grid() the scrollbar, thus the scrollbar does not appear on the text widget. Ignore what is in the variable Quote, it is just test data.
EventScrollBar= tk.Scrollbar(EventChoice)
EventText=tk.Text(EventChoice,height=25,width=50)
EventText.grid(row=3,column=1,columnspan=5)
EventScrollBar.config(command=EventText.yview)
EventText.config(yscrollcommand=EventScrollBar.set)
Quote=("""
...
wd""")
EventText.insert(tk.END,Quote)
EventText.config(state=tk.DISABLED)
I give you two ways of making a Scrollbar.
1) Using tk.Scrollbar
import tkinter as tk
root = tk.Tk()
EventText=tk.Text(root, height=10, width=50)
EventScrollBar= tk.Scrollbar(root, command=EventText.yview, orient="vertical")
EventScrollBar.grid(row=0, column=1, sticky="ns")
EventText.grid(row=0,column=0)
EventText.configure(yscrollcommand=EventScrollBar.set)
Quote=("""Suck\ne\ne\ne\ne\ne\ne\ne\ne\ne\nee\ne\ne\ne\ne\ne\ne\ne\nee\ned\ne\ne\nde\nd\ne\nded\nc\nc\nx\nc\nx\nc\nzc\ns\nds\nx\nwd\ns\nd\nwd""")
EventText.insert(tk.END,Quote)
root.mainloop()
2) Using ScrolledText
import tkinter as tk
from tkinter import scrolledtext
root = tk.Tk()
Quote=("""Suck\ne\ne\ne\ne\ne\ne\ne\ne\ne\nee\ne\ne\ne\ne\ne\ne\ne\nee\ned\ne\ne\nde\nd\ne\nded\nc\nc\nx\nc\nx\nc\nzc\ns\nds\nx\nwd\ns\nd\nwd""")
EventText = scrolledtext.ScrolledText(root, height=10, width=50)
EventText.insert("end", Quote)
EventText.grid(row=0, column=0)
root.mainloop()
Your code shows no attempt to grid the scrollbar.
See below example:
import tkinter as tk
root = tk.Tk()
ybar= tk.Scrollbar(root)
event_text=tk.Text(root, height=10, width=10)
ybar.config(command=event_text.yview)
event_text.config(yscrollcommand=ybar.set)
event_text.grid(row=0, column=0)
ybar.grid(row=0, column=1, sticky="ns")
for i in range(100):
event_text.insert("end", "{}\n".format(i))
root.mainloop()
Just in case you are using grid() in your original code and forgot it here in your example your problem is likely due to the columnspan=5.
If you do that to your text widget then it will sit on top of your scrollbar.
Try something like this when using columnspan:
import tkinter as tk
root = tk.Tk()
ybar= tk.Scrollbar(root)
event_text=tk.Text(root, height=10, width=10)
ybar.config(command=event_text.yview)
event_text.config(yscrollcommand=ybar.set)
event_text.grid(row=0, column=0, columnspan=5)
ybar.grid(row=0, column=5, sticky="ns")
for i in range(100):
event_text.insert("end", "{}\n".format(i))
root.mainloop()

Tkinter - create new window on top of root window

How do I create a new tkinter root window on top of the main root window that prevents you from accessing the main window until you close the secondary window?
Here is the code that I've written so far (in python 3.6):
from tkinter import *
root = Tk()
root.geometry('600x400+0+0')
def new_page():
rootB = Tk()
btnNP = Button(root, padx=1, pady=2, fg='black',relief='raise',font=
('garamond',10, 'italic', 'bold'),
text='New Page', bg='blue', command=new_page)
btnNP.place(x=100, y=300)
text1 = Text(root, bd=5, height=1, width=14, bg='pink')
text1.place(x=100, y=250)
root.mainloop()
I use Toplevel instead of a new root
def New_page():
popup = Toplevel()
popup.grab_set()
this should do the trick

Categories

Resources