I'm coding a GUI for inputting parameters, images and text all associated with a particular step (One single row per step) and then outputting it as JavaScript code to be used in another program. But this is beside my point:
I am having trouble forcing the column widths. It seems that both canv_1.grid_columnconfigure(6, weight=2) and canv_1.grid_columnconfigure(1, weight=0) do nothing. I'm trying to keep the width of the column 2nd from the left to a minimum. And I want column 6 to be twice as wide as the others.
import tkinter as tk
from tkinter import simpledialog,filedialog,colorchooser,messagebox,Frame,Button
from PIL import ImageTk, Image
import textwrap
root = tk.Tk()
load1 = Image.open("example.jpg")
root.render1 = ImageTk.PhotoImage(load1)
canv_1 = tk.Canvas(root, bg="gray")
canv_1.grid_rowconfigure(0, weight=1)
canv_1.grid_columnconfigure(6, weight=2)
canv_1.grid_columnconfigure(2, weight=0)
canv_1.grid(row=0, column=0, sticky="news")
canv_1.grid(row=1, column=0, sticky="news")
labels = ["Image", "Chapter #", "Chapter Title", "Step", "Slide", "Sequence Step", "Instructions"]
root.label_wid = []
font1 = ("arial", 15, "bold")
ch_text = []
def exportCode():
# Gather text input for each chapter
for i in range(0,5):
# Get the text (unwrapped) from a single input cell:
a_temp = instruction_col[i].get('1.0','10.0')
# Wrap the text at 54 characters, then append it to the list ch_text
ch_text.append(textwrap.wrap(a_temp,54))
# Create a new file
f = open("demofile2.js", "w")
# For each chapter
for i in range(1,5):
# Write to this file line by line
#
# stepID = str()
# stepHeading = "if(place==\""
for j in range(1,11):
tnum = str(j)
nline1st = " scopeSet(\"T"+tnum+"\",\"text\",\""
if (len(ch_text[i-1])<j):
nline2nd = ""
else:
nline2nd = ch_text[i-1][j-1]
f.write(nline1st+nline2nd+"\")\n")
f.close()
print("Code exported.")
for i in range(len(labels)):
root.label_wid.append(tk.Label(canv_1, font=font1, relief="raised", text=labels[i]).grid(row=0, column=i, sticky="we"))
root.images = [
ImageTk.PhotoImage(Image.open("example.jpg"))
for y in range(1,6)
]
def change_img(y):
#print(y)
#print(len(root.images))
root.img1 = filedialog.askopenfilename()
root.load_img1 = Image.open(root.img1)
root.render_img1 = ImageTk.PhotoImage(root.load_img1)
image_col[y]['image'] = root.render_img1
print(image_col[y]['image'])
print(image_col[y-1]['image'])
print(root.render_img1)
c1 = "#a9d08e"
c2 = "#8dd1bf"
image_col = [
tk.Button(
canv_1,
image=root.render1,
relief="raised",
bg="light gray",
command = lambda y=y: change_img(y),
)
for y in range(0, 5)
]
ChNum_col = [
tk.Entry(
canv_1,
bg = c1,
font = ("arial", 15))
for y in range(0, 5)
]
Title_col = [
tk.Entry(
canv_1,
bg = c1,
font = ("arial", 15))
for y in range(0, 5)
]
Step_col = [
tk.Entry(
canv_1,
bg = c1,
font = ("arial", 15))
for y in range(0, 5)
]
Slide_col = [
tk.Entry(
canv_1,
bg = c2,
font = ("arial", 15))
for y in range(0, 5)
]
SeqS_col = [
tk.Entry(
canv_1,
bg = c2,
font = ("arial", 15))
for y in range(0, 5)
]
instruction_col = [
tk.Text(
canv_1,
bg="white",
wrap="word",
font=("arial",15), width=20, height=10)
for y in range(0, 5)
]
example_text_1 = "The purpose of this app is for the user to select an image and designate the associated parameters and text before exporting it as a JavaScript file to be used in a separate program."
example_text_2 = " Here is another example of text that might be used. This can be used for a specific chapter to test this code. It's getting closer to being ready for use"
instruction_col[0].insert(1.0,example_text_1)
instruction_col[1].insert(1.0,example_text_2)
# for y, image_cell in enumerate(image_col, 1):
# image_cell.grid(row=y, column=0, sticky='news')
# for y, image_cell in enumerate(instruction_col, 1):
# image_cell.grid(row=y, column=6, sticky='news')
# above is same as below
for y in range(0,5):
image_col[y].grid(row=y + 1, column=0, sticky='news')
ChNum_col[y].grid(row=y + 1, column=1, sticky='news')
Title_col[y].grid(row=y + 1, column=2, sticky='news')
Step_col[y].grid(row=y + 1, column=3, sticky='news')
Slide_col[y].grid(row=y + 1, column=4, sticky='news')
SeqS_col[y].grid(row=y + 1, column=5, sticky='news')
instruction_col[y].grid(row=y+1, column=6, sticky='news')
# ================================================================================================
bt1 = tk.Button(canv_1, text="Export", font=font1, command=exportCode).grid(row=0, column=7)
load2 = Image.open("scroll-up.png")
root.render2 = ImageTk.PhotoImage(load2)
load3 = Image.open("scroll-down.png")
root.render3 = ImageTk.PhotoImage(load3)
scroll_up = tk.Button(canv_1, image=root.render2).grid(row=1, column=7, rowspan=2) # , sticky="n")
scroll_up = tk.Button(canv_1, image=root.render3).grid(row=3, column=7, rowspan=2) # , sticky="s")
root.mainloop()
First
As mentioned by
#jizhihaoSAMA
width must be set.
I configured all Entry widths as width = 1, that seemed to shrink the column the to minimum width that matches the width of the accompanying Label above it.
Second
In addition to weight, it seems you need uniform = 999 (or any number / string) that will identify the column as part of a group that will proportion the width according to weight = 1, wieght = 2 per respective column.
In the modified script I made column 7 as 5 times the width of column 1
import tkinter as tk
from tkinter import simpledialog,filedialog,colorchooser,messagebox,Frame,Button,PhotoImage
#from PIL import ImageTk, Image
import textwrap
root = tk.Tk()
#load1 = Image.open("example.jpg")
root.render1 = PhotoImage(file="yes.png")
canv_1 = tk.Canvas(root, bg="red")
## Here uniform = ... must be set ##
canv_1.grid_rowconfigure(0, weight=1,uniform = 999)
canv_1.grid_columnconfigure(6, weight=5, uniform = 999)
canv_1.grid_columnconfigure(2, weight=0, uniform = 999)
canv_1.grid(row=0, column=0, sticky="news")
canv_1.grid(row=1, column=0, sticky="news")
labels = ["Image", "Chapter #", "Chapter Title", "Step", "Slide", "Sequence Step", "Instructions"]
root.label_wid = []
font1 = ("arial", 15, "bold")
ch_text = []
def exportCode():
# Gather text input for each chapter
for i in range(0,5):
# Get the text (unwrapped) from a single input cell:
a_temp = instruction_col[i].get('1.0','10.0')
# Wrap the text at 54 characters, then append it to the list ch_text
ch_text.append(textwrap.wrap(a_temp,54))
# Create a new file
#f = open("demofile2.js", "w")
# For each chapter
for i in range(1,5):
# Write to this file line by line
#
# stepID = str()
# stepHeading = "if(place==\""
for j in range(1,11):
tnum = str(j)
nline1st = " scopeSet(\"T"+tnum+"\",\"text\",\""
if (len(ch_text[i-1])<j):
nline2nd = ""
else:
nline2nd = ch_text[i-1][j-1]
#f.write(nline1st+nline2nd+"\")\n")
#f.close()
print("Code exported.")
for i in range(len(labels)):
tmp = tk.Label(canv_1, font=font1, relief="raised", text=labels[i])
tmp.grid(row=0, column=i, sticky="we")
root.label_wid.append(tmp)
print(root.label_wid[0].winfo_reqwidth())
root.images = [PhotoImage(file="yes.png") for y in range(1,6)]
def change_img(y):
#print(y)
#print(len(root.images))
root.img1 = filedialog.askopenfilename()
root.load_img1 = Image.open(root.img1)
root.render_img1 = PhotoImage(root.load_img1)
image_col[y]['image'] = root.render_img1
print(image_col[y]['image'])
print(image_col[y-1]['image'])
print(root.render_img1)
c1 = "#a9d08e"
c2 = "#8dd1bf"
image_col = [ tk.Button(canv_1, image=root.render1,relief="raised",bg="light gray",command = lambda y=y: change_img(y),width = 1) for y in range(0, 5) ]
ChNum_col = [tk.Entry(canv_1,bg = c1,font = ("arial", 15),width = 1) for y in range(0, 5) ]
Title_col = [tk.Entry(canv_1,bg = c1,font = ("arial", 15),width = 1) for y in range(0, 5) ]
Step_col = [tk.Entry(canv_1,bg = c1,font = ("arial", 15),width = 1) for y in range(0, 5)]
Slide_col = [tk.Entry( canv_1, bg = c2,font = ("arial", 15),width = 1) for y in range(0, 5)]
SeqS_col = [tk.Entry(canv_1,bg = c2,font = ("arial", 15),width = 1) for y in range(0, 5)]
instruction_col = [ tk.Text( canv_1, bg="white",wrap="word", font=("arial",15), width=5, height=10) for y in range(0, 5)
]
example_text_1 = "The purpose of this app is for the user to select an image and designate the associated parameters and text before exporting it as a JavaScript file to be used in a separate program."
example_text_2 = " Here is another example of text that might be used. This can be used for a specific chapter to test this code. It's getting closer to being ready for use"
instruction_col[0].insert(1.0,example_text_1)
instruction_col[1].insert(1.0,example_text_2)
# for y, image_cell in enumerate(image_col, 1):
# image_cell.grid(row=y, column=0, sticky='news')
# for y, image_cell in enumerate(instruction_col, 1):
# image_cell.grid(row=y, column=6, sticky='news')
# above is same as below
for y in range(0,5):
image_col[y].grid(row=y + 1, column=0, sticky='news')
ChNum_col[y].grid(row=y + 1, column=1, sticky='news')
Title_col[y].grid(row=y + 1, column=2, sticky='news')
Step_col[y].grid(row=y + 1, column=3, sticky='news')
Slide_col[y].grid(row=y + 1, column=4, sticky='news')
SeqS_col[y].grid(row=y + 1, column=5, sticky='news')
instruction_col[y].grid(row=y+1, column=6, sticky='news')
# ================================================================================================
bt1 = tk.Button(canv_1, text="Export", font=font1, command=exportCode).grid(row=0, column=7)
#load2 = Image.open("scroll-up.png")
root.render2 = PhotoImage(file="yes.png")
#load3 = Image.open("scroll-down.png")
root.render3 = PhotoImage(file="yes.png")
scroll_up = tk.Button(canv_1, image=root.render2).grid(row=1, column=7, rowspan=2) # , sticky="n")
scroll_up = tk.Button(canv_1, image=root.render3).grid(row=3, column=7, rowspan=2) # , sticky="s")
root.mainloop()
Use width
I tried width earlier, but it didn't seem to work- possibly because I had empty columns? Now the code is working though:
ChNum_col = [
tk.Entry(
canv_1,
bg = c1,
width = 10,
font = ("arial", 15))
for y in range(0, 5)
]
Related
I get error like this in the title when I run this code. I want to make TODO list but I'm getting many errors on my way :(. I can't call array (tab) in other function and I can't change column in checkbox grid.
from tkinter import *
from tkinter import messagebox as msb
win = Tk()
win.title("Checkbutton")
win.geometry("300x600")
win.resizable("False","False")
task_name = StringVar()
tab = []
tab.append("")
tab.append("")
r = 2
c = 1
cc = IntVar
def checking():
tab[r].grid(row=r, column=1, padx=(10, 10), pady=(5, 5))
def addcheck():
global r
global c
global cc
if r < 16:
tab.append(Checkbutton(variable = cc, text = task_name.get(), command = checking))
tab[r].grid(row=r, column=0, padx=(10, 10), pady=(5, 5))
r = r + 1
task_entry.delete(0, 20)
else:
msb.showwarning(title="DANGER", message="TOO MUCH TASKS, TAKE A REST")
text1 = Label(win, text = "TO DO:").grid(row = 1, column = 0, padx = (10, 10), pady = (30, 10))
text2 = Label(win, text = "DONE: ").grid(row = 1, column = 1, padx = (10, 10), pady = (30, 10))
task_entry = Entry(width = "20", textvariable = task_name)
task_entry.grid(row = 0, column = 0, padx = (30, 10))
add = Button(win, text = "add task", command = addcheck).grid(row = 0, column = 1, padx = (10, 10))
mainloop()
Full error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\matib\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "D:\pythonprojekty\pythonProject\main.py", line 18, in checking
tab[r].grid(row=r, column=1, padx=(10, 10), pady=(5, 5))
IndexError: list index out of range
You're getting the IndexError because of trying to use the global variable r in the checking() function — and the way to fix it is by explicitly passing the row value as an argument when calling it. In the code below this is being done by using a lambda expression to define the callback command= function.
There were a number of other mistakes in your code, and I have attempted to correct them all and reformat the code to follow the PEP 8 - Style Guide for Python Code recommendations for readability.
import tkinter as tk
from tkinter import messagebox as msb
from tkinter.constants import *
win = tk.Tk()
win.title("Checkbutton")
win.geometry("300x600")
win.resizable(FALSE, FALSE)
task_name = tk.StringVar()
tab = [None, None]
r = len(tab)
def checking(row):
tab[row].grid(row=row, column=1, padx=(10, 10), pady=(5, 5))
def addcheck():
global r
if r > 16:
msb.showwarning(title="DANGER", message="TOO MANY TASKS, TAKE A REST")
else:
cb_var = tk.IntVar()
tab.append(tk.Checkbutton(variable=cb_var, text=task_name.get(),
command=lambda row=r: checking(row)))
tab[r].grid(row=r, column=0, padx=(10, 10), pady=(5, 5))
r += 1
task_entry.delete(0, END)
tk.Label(win, text="TO DO:").grid(row=1, column=0, padx=(10, 10), pady=(30, 10))
tk.Label(win, text="DONE: ").grid(row=1, column=1, padx=(10, 10), pady=(30, 10))
task_entry = tk.Entry(width="20", textvariable=task_name)
task_entry.grid(row=0, column=0, padx=(30, 10))
tk.Button(win, text="add task", command=addcheck).grid(row=0, column=1, padx=(10, 10))
task_entry.focus_set()
win.mainloop()
Here's a screenshot of it running on my system:
I have all the gui configurations and all that stuff in my main.py and my algorithms to draw bubble sort and merge sort in another .py file. I'm trying to write the print functions into my canvas but I'm not sure how to do it, can anyone help? I tried using a ListBox() but it messes up the main canvas for some reason.
This is my code for my main file:
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from bubbleSort import bubbleSort
from mergeSort import mergeSort
tk = Tk()
tk.title('Examen Final')
tk.maxsize(900, 600)
tk.config(bg = 'black')
algoritmo = StringVar()
data = []
def dibujar(data, color):
c.delete("all")
cHeight = 380
cWidth = 600
algoWidth = cWidth / (len(data) + 1)
algoHeight = cWidth / (len(data) + 1)
offset = 20
spacing = 10
tamData = [i / max(data) for i in data]
for i, height in enumerate(tamData):
x0 = i * algoWidth + offset + spacing
y0 = cHeight - height * 50
x1 = (i+1) * algoWidth + offset
y1 = cHeight
c.create_oval(x0,y0,x1,y1, fill = color[i])
c.create_text(x0+2,y0, anchor = SW, text=str(data[i]))
tk.update_idletasks()
def Ordenar():
print("Se selecciono: " + algoritmo.get())
print("Iniciando algoritmo")
global data
if menu.get() == 'MERGE SORT':
mergeSort(data, dibujar)
elif menu.get() == 'BUBBLE SORT':
bubbleSort(data, dibujar)
dibujar(data, ['green' for x in range(len(data))])
def agregar():
global data
input = int(inputVal.get())
inputVal.delete(0, END)
try:
print("valor input:")
print(input)
data.append((input))
print(str(data))
dibujar(data, ['red' for x in range(len(data))])
except:
messagebox.showerror("Error", "Ingrese un valor numerico")
def limpiar():
global data
data = []
c.delete("all")
print(data)
box = Frame(tk, width = 600, height = 200, bg = 'black' )
box.grid(row = 0, column = 0, padx=10, pady=5)
c = Canvas(tk, width = 600, height = 380, bg = 'grey')
c.grid(row = 1, column = 0, padx=10, pady=5)
c2 = Canvas(tk, width = 200, height = 380, bg = 'grey')
c2.grid(row = 1, column = 1, padx=10, pady=5)
label = Label(box, text='Lista Algoritmos: ', font = ("Arial",15), borderwidth=1, bg = "black" , fg = 'white')
label.grid(row=0,column=0, padx=5, pady=5, sticky = W)
menu = ttk.Combobox(box, textvariable = algoritmo, values=['BUBBLE SORT', 'MERGE SORT', 'HASH TABLES', 'ARBOL AVL', 'ARBOLES ROJO Y NEGRO'])
menu.grid(row=0, column=1, padx=5, pady=5)
menu.current(0)
botonStart = Button(box, text = 'Ordenar', command = Ordenar, bg = 'lime green')
botonStart.grid(row = 0, column = 2, padx = 5, pady = 5)
label = Label(box, text='Insertar valor: ', font = ("Arial",15), borderwidth=1, bg = "black" , fg = 'white')
label.grid(row=1,column=0, padx = 5, pady = 5, sticky = W)
inputVal = Entry(box)
inputVal.grid(row=1,column=1, padx = 5, pady = 5, sticky = W)
botonAdd = Button(box, text = 'Agregar', command = agregar, bg = 'lime green')
botonAdd.grid(row = 1, column = 2, padx = 5, pady = 5, sticky = W)
botonClear = Button(box, text = 'Limpiar', command = limpiar, bg = 'lime green')
botonClear.grid(row = 1, column = 3, padx = 5, pady = 5, sticky = W)
tk.mainloop()
and this is my bubbleSort.py
import time
def bubbleSort(data, dibujar):
for _ in range(len(data)-1):
for j in range(len(data)-1):
if data[j] > data[j+1]:
print(("El numero " + str(data[j]) + " es mayor que " + str(data[j+1])))
data[j],data[j+1] = data[j+1], data[j]
print(("Intercambiando de lugar " + str(data[j]) + " con " + str(data[j+1])))
dibujar(data,
[
'green'
if x == j or x == j+1
else 'red' for x in range(len(data))
]
)
time.sleep(1)
dibujar(data, ['green' for x in range(len(data))])
You can just pass in c2 to the function on the other file. So first define the parameter:
def bubbleSort(data, dibujar, cnv):
cnv.create_text(100,100,text='Trial Text')
....
Now each time you call this function, pass on c2 to it.
if menu.get() == 'BUBBLE SORT':
bubbleSort(data, dibujar, c2)
I did notice that you are using dynamic points for calculation, if so, make those points global, inside the function, then pass those onto the function by creating more parameter, while keeping in mind that the points have to defined before the function is called.
Im trying to append the input numbers to my array data everytime I click the button "agregar", however it seems like it only appends the value i just inserted and completely forgets about the previous append. I don't get any error messaged with that one. Also, when I click "iniciar" it should draw ovals with the values in data but I get the error
TypeError: Iniciar() missing 1 required positional argument: 'data'
I'm not sure what I'm doing wrong and would appreciate it if anyone could help.
from tkinter import *
from tkinter import ttk
from array import *
import random
tk = Tk()
tk.title('Bubble Sort')
tk.maxsize(900, 600)
tk.config(bg = 'black')
#Variables
algoritmo = StringVar()
#comandos
def dibujar(data):
c.delete("all")
cHeight = 380
cWidth = 600
#para escalar
algoWidth = cWidth / (len(data) + 1)
algoHeight = cWidth / (len(data) + 1)
offset = 20
spacing = 10
tamData = [i / max(data) for i in data]
for i, height in enumerate(tamData):
#top left
x0 = i * algoWidth + offset + spacing
y0 = cHeight - height * 50
#botom right
x1 = (i+1) * algoWidth + offset
y1 = cHeight
c.create_oval(x0,y0,x1,y1, fill = 'red')
c.create_text(x0+2,y0, anchor = SW, text=str(data[i]))
def Iniciar(data):
print("Se selecciono: " + algoritmo.get())
dibujar(a)
def agregar():
data = array('i', [1, 3, 5, 7, 9])
input = int(inputVal.get())
data.append((input))
print("valor input:")
print(input)
print(str(data))
def limpiar():
c.delete("all")
#Frame
box = Frame(tk, width = 600, height = 200, bg = 'black' )
box.grid(row = 0, column = 0, padx=10, pady=5)
c = Canvas(tk, width = 600, height = 380, bg = 'grey')
c.grid(row = 1, column = 0, padx=10, pady=5)
#UI
#Row-0
label = Label(box, text='Lista Algoritmos: ', font = ("Arial",15), borderwidth=1, bg = "black" , fg = 'white')
label.grid(row=0,column=0, padx=5, pady=5, sticky = W)
menu = ttk.Combobox(box, textvariable = algoritmo, values=['BUBBLE SORT', 'MERGE SORT', 'HASH TABLES', 'ARBOL AVL', 'ARBOLES ROJO Y NEGRO'])
menu.grid(row=0, column=1, padx=5, pady=5)
menu.current(0)
botonStart = Button(box, text = 'Iniciar', command = Iniciar, bg = 'lime green')
botonStart.grid(row = 0, column = 2, padx = 5, pady = 5)
#Row-1
label = Label(box, text='Insertar valor: ', font = ("Arial",15), borderwidth=1, bg = "black" , fg = 'white')
label.grid(row=1,column=0, padx = 5, pady = 5, sticky = W)
inputVal = Entry(box)
inputVal.grid(row=1,column=1, padx = 5, pady = 5, sticky = W)
botonAdd = Button(box, text = 'Agregar', command = agregar, bg = 'lime green')
botonAdd.grid(row = 1, column = 2, padx = 5, pady = 5, sticky = W)
botonClear = Button(box, text = 'Limpiar', command = limpiar, bg = 'lime green')
botonClear.grid(row = 1, column = 3, padx = 5, pady = 5, sticky = W)
tk.mainloop()
You are defining Iniciar here:
def Iniciar(data):
print("Se selecciono: " + algoritmo.get())
dibujar(a)
But when you call it below you aren't passing a data argument to it
botonStart = Button(box, text = 'Iniciar', command = Iniciar, bg = 'lime green')
Im making my own cypher encryption and I want to put the result into the entry field called output. Now im just using print() so I could test if I got a result. But that was only for testing. This is one of the first times I used Python so if there are some other things I could have done better please let me know :)
this is what I have so far.
from tkinter import *
#Make frame
root = Tk()
root.geometry("500x300")
root.title("Encryption Tool")
top_frame = Frame(root)
bottom_frame = Frame(root)
top_frame.pack()
bottom_frame.pack()
#Text
headline = Label(top_frame, text="Encryption Tool", fg='black')
headline.config(font=('Courier', 27))
headline.grid(padx=10, pady=10)
Key = Label(bottom_frame, text="Key:", fg='black')
Key.config(font=('Courier', 20))
Key.grid(row=1)
Text_entry = Label(bottom_frame, text="Text:", fg='black')
Text_entry.config(font=('Courier', 20))
Text_entry.grid(row=2)
Output_text = Label(bottom_frame, text="Output:", fg='black')
Output_text.config(font=('Courier', 20))
Output_text.grid(row=3)
Key_entry = Entry(bottom_frame)
Key_entry.grid(row=1, column=1)
Text_entry = Entry(bottom_frame)
Text_entry.grid(row=2, column=1)
Output_text = Entry(bottom_frame)
Output_text.grid(row=3, column=1)
#Encryption_button
def encrypt():
result = ''
text = ''
key = Key_entry.get()
text = Text_entry.get()
formule = int(key)
for i in range(0, len(text)):
result = result + chr(ord(text[i]) + formule + i * i)
result = ''
Encryption_button = Button(bottom_frame, text="Encrypt", fg='black')
Encryption_button.config(height = 2, width = 15)
Encryption_button.grid(row = 4, column = 0, sticky = S)
Encryption_button['command'] = encrypt
#Decryption_button
def decrypt():
result = ''
text = ''
key = Key_entry.get()
text = Text_entry.get()
formule = int(key)
for i in range(0, len(text)):
result = result + chr(ord(text[i]) - formule - i * i)
print(result)
result = ''
Decryption_button = Button(bottom_frame, text="Decrypt", fg="black")
Decryption_button.config(height = 2, width = 15)
Decryption_button.grid(row = 5, column = 0, sticky = S)
Decryption_button['command'] = decrypt
#Quit_button
def end():
exit()
Quit_button = Button(bottom_frame, text="Quit", fg='black')
Quit_button.config(height = 2, width = 15)
Quit_button.grid(row = 6, column = 0, sticky = S)
Quit_button['command'] = end
root.mainloop()
The most common way to do this with tkinter would be with a StringVar() object you can connect to the Entry object (Some documentation here).
output_entry_value = StringVar()
Output_text = Entry(bottom_frame, textvariable=output_entry_value)
Output_text.grid(row=3, column=1)
then you can .set() the result in the stringvar, and it will update in the entries you connected it to:
output_entry_value.set(result)
I have a long chunk of code. I do not want to paste it all here, so let me explain what I am trying to accomplish here. Based on a number provided by the user I want to create that many text boxes and then get what is entered into that text box and insert that into the dictionary. I have tried this a few ways and just cannot get it to work correctly. The list is either empty or it only contains the last text box as the value for each key.
def multiple_choice():
def add():
top.destroy()
top = Tk()
top.title("Add Question")
w = 800
h = 800
ws = top.winfo_screenwidth()
hs = top.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
top.geometry('%dx%d+%d+%d' % (w, h, x, y))
question = Label(top, text="Question to be asked?", font = "Times 14 bold", fg = "blue")
question.grid(row = 2, column = 4)
questionText = Text(top, borderwidth = 5, width=50,height=5, wrap=WORD, background = 'grey')
questionText.grid(row = 3, column = 4)
numQuestions = Label(top, text = "Number of answer choices?", font = "Times 14 bold", fg = "blue")
numQuestions.grid(row = 4, column=4)
num = Entry(top, bd = 5)
num.grid(row=5, column = 4)
answerList = {}
def multiple():
def preview():
preview = Tk()
top.title("Question Preview")
w = 500
h = 500
ws = top.winfo_screenwidth()
hs = top.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
top.geometry('%dx%d+%d+%d' % (w, h, x, y))
title = Label(preview, text = "Short Answer Question Preview", font = "Times 18 bold", fg = "blue" )
title.grid(row = 0, column = 2)
qtext = "Question text will read: "
ques = Label(preview, text = qtext)
ques.grid(row=1, column = 2)
ques2 = Label( preview, text = questionText.get("1.0",END))
let = 'A'
i = 1
for word in answerList:
prev = let + ": " + word
ans = Label(preview, text = prev)
ans.grid(row=1+i, column = 2)
let = chr(ord(let) + 1)
answerCor = "The correct answer(s): "
a = Label(preview, text = answerCor)
a.grid(row=4, column = 2)
b = Label(preview, text = cor.get)
b.grid(row=5, column = 2)
if num.get().isdigit():
number = int(num.get())
AnswerChoices = Label(top, text = "Answer Choices?", font = "Times 14 bold", fg = "blue")
AnswerChoices.grid(row = 6, column=4)
i = 0
let = 'A'
while i < number:
letter = Label(top, text = let)
letter.grid(row = 8+(i*4), column = 3)
answer = Text(top, borderwidth = 5, width=50, height=3, wrap=WORD, background = 'grey')
answer.grid(row = 8+(i*4), column = 4)
answerList[let] = answer.get("1.0",END)
i = i+1
let = chr(ord(let) + 1)
print answerList
correct = Label(top, text = "Correct Answer(s) (seperated by commas)",
font = "Times 14 bold", fg = "blue")
correct.grid(row =99 , column=4)
cor = Text(top, borderwidth = 5, width=50, height=3, wrap=WORD, background = 'grey')
cor.grid(row=100, column = 4)
else:
error = Tk()
w = 500
h = 100
ws = top.winfo_screenwidth()
hs = top.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
error.geometry('%dx%d+%d+%d' % (w, h, x, y))
text = "ERROR: You must enter an integer number"
Label(error,text = text, fg = "red", font = "Times 16 bold").pack()
MyButton5 = Button(top, text="Preview Question", width=20, command = preview, anchor=S)
MyButton5.grid(row=102, column=5)
MyButton4 = Button(top, text="Finish and Add Question", width=20, command = add, anchor=S)
MyButton4.grid(row=102, column=2)
but = Button(top, text="Submit", width=10, command = multiple)
but.grid(row=6, column = 4)
top.mainloop()
def button():
MyButton21 = Button(quiz, text="Short Answer Question", width=20, command = short_answer)
MyButton21.grid(row=8, column=2)
MyButton22 = Button(quiz, text="True/False Question", width=20, command = true_false)
MyButton22.grid(row=8, column=4)
MyButton23 = Button(quiz, text="Multiple Choice Question", width=20, command = multiple_choice)
MyButton23.grid(row=9, column=2)
#MyButton24 = Button(quiz, text="Matching Question", width=20, command = matching)
#MyButton24.grid(row=9, column=4)
MyButton25 = Button(quiz, text="Ordering Question", width=20, command =order)
MyButton25.grid(row=10, column=2)
MyButton26 = Button(quiz, text="Fill in the Blank Question", width=20, command = fill_blank)
MyButton26.grid(row=10, column=4)
MyButton3 = Button(quiz, text="Finsh Quiz", width=10, command = quiz)
MyButton3.grid(row=12, column=3)
quiz = Tk()
w = 700
h = 300
ws = quiz.winfo_screenwidth()
hs = quiz.winfo_screenheight()
x = 0
y = 0
quiz.geometry('%dx%d+%d+%d' % (w, h, x, y))
quiz.title("eCampus Quiz Developer")
L1 = Label(quiz, text="Quiz Title?")
L1.grid(row=0, column=0)
E1 = Entry(quiz, bd = 5)
E1.grid(row=0, column=3)
name_file = E1.get()
name_file = name_file.replace(" ", "")
name_file = name_file + ".txt"
with open(name_file,"w") as data:
MyButton1 = Button(quiz, text="Submit", width=10, command = button)
MyButton1.grid(row=1, column=3)
quiz.mainloop()
I am trying to create the dictionary using this chunk of code:
i = 0
let = 'A'
while i < number:
letter = Label(top, text = let)
letter.grid(row = 8+(i*4), column = 3)
answer = Text(top, borderwidth = 5, width=50, height=3, wrap=WORD, background = 'grey')
answer.grid(row = 8+(i*4), column = 4)
answerList[let] = answer.get("1.0",END)
i = i+1
let = chr(ord(let) + 1)
I have even tried putting a loop in the preview function but that is when the last box was the only value contained in the dictionary. Any ideas would be appreciated
Please see my commeneted snippet below which demonstrates this:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.entry = Entry(self.root) #entry to input number of entries later
self.button = Button(self.root, text="ok", command=self.command) #calls command to draw the entry boxes
self.frame = Frame(self.root)
self.entry.pack()
self.button.pack()
self.frame.pack()
def command(self):
self.frame.destroy() #destroys frame and contents
self.frame = Frame(self.root) #recreates frame
self.text = [] #empty array to store entry boxes
for i in range(int(self.entry.get())):
self.text.append(Entry(self.frame, text="Question "+str(i))) #creates entry boxes
self.text[i].pack()
self.done = Button(self.frame, text="Done", command=self.dict) #button to call dictionary entry and print
self.done.pack()
self.frame.pack()
def dict(self):
self.dict = {}
for i in range(len(self.text)):
self.dict.update({self.text[i].cget("text"): self.text[i].get()})
#the above line adds a new dict entry with the text value of the respective entry as the key and the value of the entry as the value
print(self.dict) #prints the dict
root = Tk()
App(root)
root.mainloop()
The above asks the user how many entry widgets they want to create, fills a frame with those entry widgets and then iterates through the list which contains them on the press of a button, adding a new dictionary entry for each entry widget, where the key of the dict is the attribute text for each entry and the value of the dict is the value of each entry.