tkinter clickable Label to open details of Label [duplicate] - python

This question already has answers here:
tkinter creating buttons in for loop passing command arguments
(3 answers)
Closed 6 months ago.
I'm trying to program an interface with tkinter, that shows users of my application as a list of Labels. The list is created from a database (created with sqlite3).
By clicking on each Label the program should open another window, showing details of the clicked user.
maybe do I need to use classes? (because for now, my script is not OOP)
root = Tk()
root.title("Utenti")
root.iconbitmap("D:\Willow\WilloW SW Gestionale")
root.geometry("365x600")
def open_user(name = str):
user = Tk()
user.title("Utenti")
user.iconbitmap("D:\Willow\WilloW SW Gestionale")
user.geometry("500x400")
#create entry
f_name_entry = Entry(user, width=30, state="disabled")
f_name_entry.grid(row=0, column=1, padx=20, pady=(10, 0))
P_iva_entry = Entry(user, width=30, state="disabled")
P_iva_entry.grid(row=1, column=1)
cell_number_entry = Entry(user, width=30, state="disabled")
cell_number_entry.grid(row=2, column=1)
tax_entry = Entry(user, width=30, state="disabled")
tax_entry.grid(row=3, column=1)
inps_entry = Entry(user, width=30, state="disabled")
inps_entry.grid(row=4, column=1)
# create text boxes
f_name_label = Label(user, text=name)
f_name_label.grid(row=0, column=0, pady=(10, 0))
P_iva_label = Label(user, text="p iva")
P_iva_label.grid(row=1, column=0)
cell_number_label = Label(user, text="cell number")
cell_number_label.grid(row=2, column=0)
tax_label = Label(user, text="tax")
tax_label.grid(row=3, column=0)
inps_label = Label(user, text="inps")
inps_label.grid(row=4, column=0)
i = 4
conn = sqlite3.connect("usersEsteso.db")
c = conn.cursor()
c.execute("SELECT *, oid FROM usersEsteso")
records = c.fetchall()
for record in records:
print_records = record[0]
users_lable = Label(root, bg="#778899")
users_lable.grid(row=i + 7, column=0, sticky="w", padx=5, pady=5, columnspan=3)
user_text = Label(users_lable, text=print_records)
user_text.grid(row=0, column=0, pady=5, padx=5, sticky="w")
#user_text.bind("<Enter>", open_user("sam"))
openUser_lable_button = Button(users_lable, text="apri " + record[0], command=lambda: open_user(record[0])) #here i showld pass the identification of the parent the button
openUser_lable_button.grid(row=1, column=0, pady=5, padx=5, sticky="e")
i = i + 1
conn.commit()
conn.close()

here the previous code, without database and simplyfied:
from tkinter import *
root = Tk()
root.title("Users")
root.geometry("365x600")
#global variables
i = 1
def open_user():
user = Tk()
user.title("User")
user.geometry("500x400")
#create entry
f_name_entry = Entry(user, width=150, state="normal")
f_name_entry.grid(row=0, column=1, padx=20, pady=(10, 0))
P_iva_entry = Entry(user, width=150, state="disabled")
P_iva_entry.grid(row=1, column=1, columnspan=2)
cell_number_entry = Entry(user, width=150, state="disabled")
cell_number_entry.grid(row=2, column=1, columnspan=2)
tax_entry = Entry(user, width=150, state="disabled")
tax_entry.grid(row=3, column=1, columnspan=2)
inps_entry = Entry(user, width=150, state="disabled")
inps_entry.grid(row=4, column=1, columnspan=2)
# create text boxes
f_name_label = Label(user, text="name")
f_name_label.grid(row=0, column=0, pady=(10, 0))
P_iva_label = Label(user, text="p iva")
P_iva_label.grid(row=1, column=0)
cell_number_label = Label(user, text="cell number")
cell_number_label.grid(row=2, column=0)
tax_label = Label(user, text="tax")
tax_label.grid(row=3, column=0)
inps_label = Label(user, text="inps")
inps_label.grid(row=4, column=0)
f_name_entry.insert(0, "here should be the name of the clicked user")
my_list = ["andrew", "sam", "Zoe"]
title_label = Label(root, text="List of Users")
title_label.grid(row=0, column=0, pady=(10,40))
for item in my_list:
print(i)
my_frame = Frame(root, bg="#a6a6a6")
my_label = Label(my_frame, text=item)
my_button = Button(my_frame, text="inspect user", command=open_user)
my_frame.grid(row=i, column=0, padx=10, pady=10)
my_label.grid(row=0, column=0, padx=10, pady=10)
my_button.grid(row=1, column=0, padx=10, pady=10)
i = i+1
root.mainloop()

Related

Getting multiple results with one button (python/ tkiner)

I'm a beginner, and I've gathered a number of reference sites to make code!
But if you click the button, you'll see the value only for the last item.
Do you happen to know how to get each result in every row! And now you have to enter all the items to get the value, but even if there is a blank, I hope the result will come out for all the input boxes.
I think the code is a little tangled, but it's hard to solve. I'm posting the code's full text.
from tkinter import *
from tkinter import ttk
import csv
class App(ttk.Frame):
num_rows = 2
entry_list = []
def __init__(self, master, *args, **kwargs):
ttk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.label_frame = ttk.Frame(self.master)
self.label_frame.grid()
self.label_list = []
label1 = Label(root, width=5, text="No.")
label2 = Label(root, width=12, text="Chip")
label3 = Label(root, width=12, text="Length[mm] : ")
label4 = Label(root, width=12, text="Width[mm] : ")
label5 = Label(root, width=12, text="Height[mm] : ")
label6 = Label(root, width=12, text="Watt[W] : ")
label7 = Label(root, width=12, text="Rjc[C/W] : ")
label8 = Label(root, width=12, text="h[W/㎡K] : ")
label9 = Label(master, width=12, text="Temp[C] : ",fg='red')
#grid
label1.grid(row=0, column=0, padx=1, pady=10)
label2.grid(row=0, column=1, padx=2, pady=10)
label3.grid(row=0, column=2, padx=2, pady=10)
label4.grid(row=0, column=3, padx=2, pady=10)
label5.grid(row=0, column=4, padx=2, pady=10)
label6.grid(row=0, column=5, padx=2, pady=10)
label7.grid(row=0, column=6, padx=2, pady=10)
label8.grid(row=0, column=7, padx=2, pady=10)
label9.grid(row=0, column=8, padx=2, pady=10)
##유동
self.Number_field = Entry(root, width=5)
self.Length_field = Entry(root, width=9)
self.Width_field = Entry(root, width=9)
self.Height_field = Entry(root, width=9)
self.Watt_field = Entry(root, width=9)
self.Rjc_field = Entry(root, width=9)
self.Temperature_field = Entry(root, width=9)
self.Number_field.grid(row=1, column=0, padx=1, pady=1)
self.Length_field.grid(row=1, column=2, padx=1, pady=1)
self.Width_field.grid(row=1, column=3, padx=1, pady=1)
self.Height_field.grid(row=1, column=4, padx=1, pady=1)
self.Watt_field.grid(row=1, column=5, padx=1, pady=1)
self.Rjc_field.grid(row=1, column=6, padx=1, pady=1)
self.Temperature_field.grid(row=1, column=8, padx=1, pady=1)
strs = StringVar()
strs2 = StringVar()
combx1 = ttk.Combobox(root, width=9, textvariable=strs)
combx1['values'] = (' Natural', ' Forced(Fan)')
combx1.current()
combx1.grid(column=7, row=1)
combx2 = ttk.Combobox(root, width=9, textvariable=strs2)
combx2['values'] = ('CPU', 'eMMC', 'PMIC')
combx2.grid(column=1, row=1)
combx2.current()
# Create a Submit Button and attached
button1 = Button(root, text="Submit", bg="black", fg="white", command=self.cal_Temp)
button1.grid(row=100, column=0, pady=10)
button2 = Button(root, text="Clear", fg="blue", command=self.clear_all)
button2.grid(row=100, column=1, pady=10)
button3 = Button(root, text="Add Row (+)", command=self.add_new)
button3.grid(row=102, column=0, pady=10)
button4 = Button(root, text="Delete Row (-)", command=self.delete)
button4.grid(row=102, column=1, pady=10)
button5 = Button(root, text="Export", command=self.writeToFile)
button5.grid(row=103, column=1, pady=10)
def writeToFile(self):
with open('Temperature_MJH.csv', 'a') as f:
w=csv.writer(f, quoting=csv.QUOTE_ALL)
w.writerow([self.Length_field.get()])
w.writerow([self.Width_field.get()])
w.writerow([self.Height_field.get()])
w.writerow([self.Watt_field.get()])
w.writerow([self.Rjc_field.get()])
w.writerow([self.Temperature_field.get()])
def clear_all(self):
self.Number_field.delete(0,END)
self.Length_field.delete(0, END)
self.Width_field.delete(0, END)
self.Height_field.delete(0, END)
self.Watt_field.delete(0, END)
self.Rjc_field.delete(0, END)
self.Temperature_field.delete(0, END)
#이거 지워도 되는지
self.Length_field.focus_set()
def cal_Temp(self):
Length = float(self.Length_field.get())
Width = float(self.Width_field.get())
Height = float(self.Height_field.get())
Watt = float(self.Watt_field.get())
Rjc = float(self.Rjc_field.get())
h = 1
Temperature = float(((Watt * 0.5) / (((Length * Width * 2) + (Length * 2) + (
Height * Width * 5)) / 1000)) / h + Rjc )
self.Temperature_field.insert(10,Temperature)
print(Temperature)
def add_new(self):
self.num_rows += 1
self.Number_field = Entry(root, width=5)
self.Length_field = Entry(root, width=9)
self.Width_field = Entry(root, width=9)
self.Height_field = Entry(root, width=9)
self.Watt_field = Entry(root, width=9)
self.Rjc_field = Entry(root, width=9)
self.Temperature_field = Entry(root, width=9)
self.Number_field.grid(row=self.num_rows, column=0, padx=1, pady=1)
self.Length_field.grid(row=self.num_rows, column=2, padx=1, pady=1)
self.Width_field.grid(row=self.num_rows, column=3, padx=1, pady=1)
self.Height_field.grid(row=self.num_rows, column=4, padx=1, pady=1)
self.Watt_field.grid(row=self.num_rows, column=5, padx=1, pady=1)
self.Rjc_field.grid(row=self.num_rows, column=6, padx=1, pady=1)
self.Temperature_field.grid(row=self.num_rows, column=8, padx=1, pady=1)
strs = StringVar()
strs2 = StringVar()
self.combx1 = ttk.Combobox(root, width=9, textvariable=strs)
self.combx1['values'] = (' Natural', ' Forced(Fan)')
self.combx1.current()
self.combx1.grid(row=self.num_rows, column=7)
self.combx2 = ttk.Combobox(root, width=9, textvariable=strs2)
self.combx2['values'] = ('CPU', 'eMMC', 'PMIC')
self.combx2.grid(row=self.num_rows, column=1)
self.combx2.current()
self.ics=self.num_rows
self.Number_field.insert(10, self.ics-1)
def delete(self):
self.num_rows -= 1
self.Number_field.destroy()
self.Length_field.destroy()
self.Width_field.destroy()
self.Height_field.destroy()
self.Watt_field.destroy()
self.Rjc_field.destroy()
self.Temperature_field.destroy()
self.combx1.destroy()
self.combx2.destroy()
self.ics -= 1
if __name__ == "__main__":
root = Tk()
root.configure(background='snow')
root.geometry("1000x450")
root.title("Expectation of Temp Calculator")
my_app = App(root)
root.mainloop()

Adjusting location of Entry in Tkinter

I am trying to get the text username and password in the below Tkinter to be next to the Entry, I have tried to play with the columns but it didn't fix it. I was previously using pack() but switched to grid() to be more in control of the location of the labels.
Here is the code:
root = Tk()
root.title("Bookmark Search")
root.resizable(0,0)
greeting= Label(root, text="Hi, This is a Trial to see how the label works !")
help=Label(root, text=" How can I help you today?")
greeting.grid(row = 0, column= 1, pady=5, padx=200)
help.grid(row = 1, column= 1,pady=5)
e = Entry(root, width=50)
e.grid(row=2, column = 1,rowspan=2, pady=15)
mySubmit = Label(root)
-------several lines of unrelated code-------
mySubmit.bind("<Button-1>", open_url)
root.bind('<Return>', myClick)
myButton= Button(root, text="Submit", command=myClick)
myButton.grid(row=4, column = 1,rowspan=2, pady=10)
# username
username_label = Label(root, text="Username:")
username_label.grid(column=0, row=8, padx=5, pady=5)
username_entry = Entry(root)
username_entry.grid(column=1, row=8, padx=5, pady=5)
# password
password_label = Label(root, text="Password:")
password_label.grid(column=0, row=9, padx=5, pady=5)
password_entry = Entry(root)
password_entry.grid(column=1, row=9, padx=5, pady=5)
# login button
login_button = Button(root, text="Login")
login_button.grid(column=1, row=10,padx=5, pady=5)
root.mainloop()
Here is a print screen of the current output:
Here is the required output:
You can put Labels and Buttons in Frame and then Frame put in main window.
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions --- (PEP8: lower_case_names)
def open_url():
pass
def my_click():
pass
# --- main ---
root = tk.Tk()
root.title("Bookmark Search")
root.resizable(0,0)
greeting = tk.Label(root, text="Hi, This is a Trial to see how the label works !")
help = tk.Label(root, text=" How can I help you today?")
greeting.grid(row=0, column=1, pady=5, padx=200)
help.grid(row=1, column=1, pady=5)
e = tk.Entry(root, width=50)
e.grid(row=2, column=1, pady=15)
my_submit = tk.Label(root, text='LABEL')
my_submit.grid(row=3, column=1, pady=10)
my_submit.bind("<Button-1>", open_url)
root.bind('<Return>', my_click)
my_button = tk.Button(root, text="Submit", command=my_click)
my_button.grid(row=4, column=1, pady=10)
# - start Frame - to group widgets -
frame = tk.Frame(root)
frame.grid(column=1, row=5)
# username
username_label = tk.Label(frame, text="Username:")
username_label.grid(column=0, row=8, padx=5, pady=5)
username_entry = tk.Entry(frame)
username_entry.grid(column=1, row=8, padx=5, pady=5)
# password
password_label = tk.Label(frame, text="Password:")
password_label.grid(column=0, row=9, padx=5, pady=5)
password_entry = tk.Entry(frame)
password_entry.grid(column=1, row=9, padx=5, pady=5)
# - end Frame -
# login button
login_button = tk.Button(root, text="Login")
login_button.grid(column=1, row=10, padx=5, pady=5)
root.mainloop()
PEP 8 -- Style Guide for Python Code

how to skip an error in this code?? (Tkinter and openpyxl involved)

thanks for helping!
I have a spreadsheet that records my income and expenses, with dates, description, debit and credit amount. I wanted to used openpyxl to automate the process of adding values that are in a particular category, the program kind of works now, but the problem is: when I am searching for a phrase that don't exist in the sheet, the program crash, it can not do the things it suppose to before and after the search phrase that dont exist.
for example,
when i wanted to calculate the subtotal of search phrase "wage" and the put the subtotal into a target cell, it works fine. the problem comes out when i ask the program to look for something that isnt there.
I ask the program to look for wage, and create a subtotal, works fine and suppose to store the value at a defined target, when i ask the program to look for tax (which dont exist) the program showed nothing, then i ask the program to get the subtotal of rent (which exists). The program cant make the changes.
i am relatively new to all this.... so thanks again for helping! :)
from tkinter import *
import openpyxl as xl
window = Tk()
window.title("Excel Automation") # rename the title
def define_subtotal():
file_name = str(file_name_input_field.get()) # Collects the text from the text entry box
sheet_name = str(sheet_name_label_name_input_field.get()) # Collects the text from the text entry box
global wb # declare Workbook as global variable
wb = xl.load_workbook(file_name) # define workbook
sheet = wb[sheet_name] # Define sheet name
col_num = int(search_column_input_field.get())
search_phrase = search_phrase_input_field.get()
offset_col = int(offset_col_input_field.get())
target_col_num = int(target_col_input_field.get())
target_row_num = int(target_row_input_field.get())
total = 0
for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, col_num)
if cell.value is None:
continue
else:
if search_phrase.casefold() in str(cell.value).casefold():
total += cell.offset(column=offset_col).value
total_description = sheet.cell(target_row_num, target_col_num + 1)
total_description.value = "Subtotal of : " + search_phrase
total_cell = sheet.cell(target_row_num, target_col_num)
total_cell.value = total
output.delete(0.0, END)
output.insert(END, "Subtotal for " + search_phrase + " defined")
else:
continue
def execute():
output.delete(0.0, END) # clear the text box
new_file_name = new_file_input_field.get()
output.insert(END, "Calculations complete!")
wb.save(new_file_name + ".xlsx")
def import_excel_file():
output.delete(0.0, END) # clear the text box
output.insert(END, "File imported")
sheet_name_label_name_input_field.config (state='disabled')
import_button.config (state='disabled')
file_name_input_field.config (state='disabled')
def close_window(): # exit function
window.destroy()
exit()
### CONTENTS
file_name_label = Label(window, text="File Name:")
file_name_input_field = Entry(window, width=38, borderwidth=2)
sheet_name_label = Label(window, text="Sheet Name:")
sheet_name_label_name_input_field = Entry(window, width=38, borderwidth=2)
import_button = Button(window, text="Import", padx=35, pady=0, command=import_excel_file)
search_phrase_label = Label(window, text="Search Phrase:")
search_phrase_input_field = Entry(window, width=38, borderwidth=2)
search_column_label = Label(window, text="Search Column:")
search_column_input_field = Entry(window, width=38, borderwidth=2)
offset_col_label = Label(window, text="Offset Column:")
offset_col_input_field = Entry(window, width=38, borderwidth=2)
target_col_label = Label(window, text="Target Column:")
target_col_input_field = Entry(window, width=38, borderwidth=2)
target_row_label = Label(window, text="Target Row:")
target_row_input_field = Entry(window, width=38, borderwidth=2)
new_file_label = Label(window, text="Name of New file:")
new_file_input_field = Entry(window, width=38, borderwidth=2)
define_subtotal_button = Button(window, text="Define Subtotal", padx=5, pady=0, command=define_subtotal)
execute_button = Button(window, text="Execute", padx=5, pady=0, command=execute)
# contents Column 2
status_label = Label(window, text="Status:")
output = Text(window, width=50, height=25, wrap=WORD, bg="white") # wrap=WORD : wrap text when in overflow.
output.insert(END, "Drag and drop file into project file\n"
"Define File Name and Sheet Name\n"
"Example: filename.xlsx /.xlsm/ xltx/.xltm")
exit_button = Button(window, text="exit", width=14, command=close_window)
### THE GRID
file_name_label.grid(row=0, column=0, columnspan=2, padx=0, pady=0, sticky=W)
file_name_input_field.grid(row=1, column=0, columnspan=2, padx=5, pady=3, sticky=W)
sheet_name_label.grid(row=2, column=0, columnspan=2, padx=0, pady=0, sticky=W)
sheet_name_label_name_input_field.grid(row=3, column=0, columnspan=2, padx=5, pady=3, sticky=W)
import_button.grid(row=4, column=0, columnspan=2, sticky=W, padx=5)
search_phrase_label.grid(row=5, column=0, columnspan=2, padx=0, pady=0, sticky=W)
search_phrase_input_field.grid(row=6, column=0, columnspan=2, padx=5, pady=5, sticky=W)
search_column_label.grid(row=7, column=0, columnspan=2, padx=0, pady=0, sticky=W)
search_column_input_field.grid(row=8, column=0, columnspan=2, padx=5, pady=5, sticky=W)
offset_col_label.grid(row=9, column=0, columnspan=2, padx=0, pady=0, sticky=W)
offset_col_input_field.grid(row=10, column=0, columnspan=2, padx=5, pady=5, sticky=W)
target_col_label.grid(row=11, column=0, columnspan=2, padx=0, pady=0, sticky=W)
target_col_input_field.grid(row=12, column=0, columnspan=2, padx=5, pady=5, sticky=W)
target_row_label.grid(row=13, column=0, columnspan=2, padx=0, pady=0, sticky=W)
target_row_input_field.grid(row=14, column=0, columnspan=2, padx=5, pady=5, sticky=W)
new_file_label.grid(row=15, column=0, columnspan=2, padx=0, pady=0, sticky=W)
new_file_input_field.grid(row=16, column=0, columnspan=2, padx=5, pady=5, sticky=W)
define_subtotal_button.grid(row=17, column=0, sticky=W, padx=5)
# GRID column 1
execute_button.grid(row=17, column=1, sticky=W, padx=5)
# GRID Column 2
status_label.grid(row=0, column=2, padx=5, sticky=W)
output.grid(row=1, column=2, rowspan=25, padx=5, sticky=NE)
exit_button.grid(row=17, column=2, sticky=E)
window.mainloop()
[enter image description here][1]
may be it will be a good option to use
try:
your code
except:
pass
And run your code inside of this from which you want to skip the error with this try except. if any exception happened then it will ignore this and will be running.

How to update Labels in tkinter?

I'm trying to create a program in where you put a word in a box, press add, and this word goes to a list, which is also displayed on the right side. When I press the forward button the first thing on the list is deleted. Problem is I can't get the labels to update when I press the buttons / edit the list.
from tkinter import *
root = Tk()
root.title('Speakers List')
root.minsize(800, 600)
speakers = ['none']
spe = speakers[0]
def add():
if spe == 'none':
speakers.insert(0, [s])
e.delete(0, END)
spe.config(text=speakers[0])
else:
speakers[-2] = [s]
e.delete(0, END)
spe.config(text=speakers[0])
return
def forward():
if len(speakers) is 0:
return
else:
del speakers[0]
spe.config(text=speakers[0])
return
entry = StringVar()
e = Entry(root, width=30, font=("Arial", 20), textvariable=entry)
e.grid(row=0, sticky=W)
s = e.get()
button1 = Button(root, padx=10, pady=10, bd=5, text='Add', fg='black', command=add)
button1.grid(row=0, column=1)
button2 = Button(root, padx=10, pady=10, bd=5, text='Next', fg='black', command=forward)
button2.grid(row=1, column=1)
n = Label(root, font=("Arial", 35), bd=2, text=spe)
n.grid(row=1, sticky=W)
listdisplay = Label(root, font=('Arial', 20), text=speakers)
listdisplay.grid(row=0, column=10)
root.mainloop()
Is this the sort of thing you were looking for ?
from tkinter import *
root = Tk()
root.title('Speakers List')
root.minsize(800, 600)
speakers = ['50']
spe = speakers[0]
def add():
entry=e.get()
speakers.append(entry)
listdisplay.config(text=speakers)
return
def forward():
if len(speakers) is 0:
return
else:
del speakers[0]
listdisplay.config(text=speakers)
spe=speakers[0]
n.config(text=spe)
return
entry = StringVar()
e = Entry(root, width=30, font=("Arial", 20), textvariable=entry)
e.grid(row=0, sticky=W)
s = e.get()
button1 = Button(root, padx=10, pady=10, bd=5, text='Add', fg='black',command=add)
button1.grid(row=0, column=1)
button2 = Button(root, padx=10, pady=10, bd=5, text='Next', fg='black',command=forward)
button2.grid(row=1, column=1)
n = Label(root, font=("Arial", 35), bd=2, text=spe)
n.grid(row=1, sticky=W)
listdisplay = Label(root, font=('Arial', 20), text=speakers)
listdisplay.grid(row=0, column=10)
root.mainloop()
If so:
You create a list and then you use the append function to add an item to it. The rest was pretty much right.

in the tkinter module, How would I make the gui wait until input has been registered

I have this as my code, and roughly halfway through is a while loop waiting until the variable self.sub is 1. this is giving me issues, as it will no longer run. it will say its running, but its not. I would be more specific, but I don't know how.
from tkinter import *
root = Tk()
text_width = 100
def destroy_root():
try:
root.destroy()
except:
a = 0
class Application(Frame):
sub = 0
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def submit(self):
if len(self.entry.get()) != 0:
self.text.config(width=text_width, state=NORMAL)
self.text.insert(END, '\n'+(str(self.entry.get())))
self.entry.delete(0, END)
self.text.config(width=text_width, state=DISABLED)
self.text.see(END)
self.sub = 1
def story(self):
self.text.config(state=NORMAL)
self.text.insert(END, '\nWelcome to the interactive, text-based game of Dimensia!\n\n_-_-_-_-_-_-_-_-_\n\n'
'Where reality is not what it seems, and parallels are more familiar than ever\n\n')
self.text.config(state=DISABLED)
self.sub = 0
condition = True
while condition:
if self.sub == 1:
condition = False
self.text.config(state=NORMAL)
self.text.insert(END, 'Hello')
self.text.config(state=DISABLED)
self.sub = 0
def createWidgets(self):
self.text = Text(self)
self.text.grid(row=0, column=0, columnspan=2, rowspan=2)
self.text.config(width=text_width, height=45, state=DISABLED)
self.border = Label(self)
self.border.grid(row=0, column=2, rowspan=2)
self.border.config(height=43, background='black')
instruction = 'Welcome to your personal scratchpad!!\n\n'
self.scratch_pad = Text(self)
self.scratch_pad.grid(row=1, column=3, columnspan=2, rowspan=2)
self.scratch_pad.config(width=50, height=30)
self.scratch_pad.insert(END, 'Scratch Pad\n_-_-_-_-_-_-_-_-_-_-_-_-_\n\n\n\n'+str(instruction))
self.QUIT = Button(self, command=self.quit)
self.QUIT.grid(row=2, column=0)
self.QUIT.config(width=1, text='Q')
self.entry = Entry(self)
self.entry.grid(row=2, column=1, columnspan=3)
self.entry.config(width=120)
self.submit_b = Button(self, text='Submit')
self.submit_b.config(command=self.submit)
self.submit_b.grid(row=2, column=4)
frame = Frame(self)
frame.grid(row=0, column=2, columnspan=2)
frame.config(height=1)
health_label = Label(frame, text='Health')
health_label.grid(row=0, column=0)
stamina_label = Label(frame, text='Stamina')
stamina_label.grid(row=2, column=0)
mana_label = Label(frame, text='Mana')
mana_label.grid(row=4, column=0)
courage_label = Label(frame, text='Courage')
courage_label.grid(row=6, column=0)
energy_label = Label(frame, text='Energy')
energy_label.grid(row=0, column=1)
strength_label = Label(frame, text='Strength')
strength_label.grid(row=2, column=1)
knowledge_label = Label(frame, text='Knowledge')
knowledge_label.grid(row=4, column=1)
confusion_label = Label(frame, text='Confusion')
confusion_label.grid(row=6, column=1)
inteligence_label = Label(frame, text='Inteligence')
inteligence_label.grid(row=0, column=2)
agility_label = Label(frame, text='Agility')
agility_label.grid(row=2, column=2)
hunger_label = Label(frame, text='Hunger')
hunger_label.grid(row=4, column=2)
coins_label = Label(frame, text='Coins')
coins_label.grid(row=6, column=2)
curiosity_label = Label(frame, text='Curiosity')
curiosity_label.grid(row=0, column=3)
fear_label = Label(frame, text='Fear')
fear_label.grid(row=2, column=3)
health_value = Spinbox(frame, text='Health', width=5)
health_value.grid(row=1, column=0)
health_value.config(from_=0, to_=100, state=DISABLED)
stamina_value = Spinbox(frame, text='Stamina', width=5)
stamina_value.grid(row=3, column=0)
stamina_value.config(from_=0, to_=100, state=DISABLED)
mana_value = Spinbox(frame, text='Mana', width=5)
mana_value.grid(row=5, column=0)
mana_value.config(from_=0, to_=100, state=DISABLED)
courage_value = Spinbox(frame, text='Courage', width=5)
courage_value.grid(row=7, column=0)
courage_value.config(from_=0, to_=100, state=DISABLED)
strength_value = Spinbox(frame, text='Strength', width=5)
strength_value.grid(row=3, column=1)
strength_value.config(from_=0, to_=100, state=DISABLED)
knowledge_value = Spinbox(frame, text='Knowledge', width=5)
knowledge_value.grid(row=5, column=1)
knowledge_value.config(from_=0, to_=100, state=DISABLED)
confusion_value = Spinbox(frame, text='Confusion', width=5)
confusion_value.grid(row=7, column=1)
confusion_value.config(from_=0, to_=100, state=DISABLED)
energy_value = Spinbox(frame, text='Energy', width=5)
energy_value.grid(row=1, column=1)
energy_value.config(from_=0, to_=100, state=DISABLED)
inteligence_value = Spinbox(frame, text='Inteligence', width=5)
inteligence_value.grid(row=1, column=2)
inteligence_value.config(from_=0, to_=100, state=DISABLED)
agility_value = Spinbox(frame, text='Agility', width=5)
agility_value.grid(row=3, column=2)
agility_value.config(from_=0, to_=100, state=DISABLED)
hunger_value = Spinbox(frame, text='Hunger', width=5)
hunger_value.grid(row=5, column=2)
hunger_value.config(from_=0, to_=100, state=DISABLED)
coins_value = Spinbox(frame, text='Coins', width=5)
coins_value.grid(row=7, column=2)
coins_value.config(from_=0, to_=100, state=DISABLED)
curiosity_value = Spinbox(frame, text='Curiosity', width=5)
curiosity_value.grid(row=1, column=3)
curiosity_value.config(from_=0, to_=100, state=DISABLED)
fear_value = Spinbox(frame, text='Fear', width=5)
fear_value.grid(row=3, column=3)
fear_value.config(from_=0, to_=100, state=DISABLED)
health_value.config(state=NORMAL)
health_value.config(value=100)
health_value.config(state=DISABLED)
stamina_value.config(state=NORMAL)
stamina_value.config(value=100)
stamina_value.config(state=DISABLED)
mana_value.config(state=NORMAL)
mana_value.config(value=0)
mana_value.config(state=DISABLED)
mana_label.config(state=DISABLED)
courage_value.config(state=NORMAL)
courage_value.config(value=50)
courage_value.config(state=DISABLED)
strength_value.config(state=NORMAL)
strength_value.config(value=35)
strength_value.config(state=DISABLED)
knowledge_value.config(state=NORMAL)
knowledge_value.config(value=5)
knowledge_value.config(state=DISABLED)
confusion_value.config(state=NORMAL)
confusion_value.config(value=75)
confusion_value.config(state=DISABLED)
energy_value.config(state=NORMAL)
energy_value.config(value=100)
energy_value.config(state=DISABLED)
inteligence_value.config(state=NORMAL)
inteligence_value.config(value=50)
inteligence_value.config(state=DISABLED)
agility_value.config(state=NORMAL)
agility_value.config(value=50)
agility_value.config(state=DISABLED)
hunger_value.config(state=NORMAL)
hunger_value.config(value=50)
hunger_value.config(state=DISABLED)
coins_value.config(state=NORMAL)
coins_value.config(value=150)
coins_value.config(state=DISABLED)
curiosity_value.config(state=NORMAL)
curiosity_value.config(value=50)
curiosity_value.config(state=DISABLED)
fear_value.config(state=NORMAL)
fear_value.config(value=25)
fear_value.config(state=DISABLED)
self.story()
a = 0
root.wm_title('Dimensia')
app = Application(master=root)
app.mainloop()
destroy_root()
GUIs are already in a perpetual state of waiting for conditions -- this is called the event loop. So, you shouldn't ever need to put in your own loop that waits for something. In fact, your code is waiting, even before your while loop. It waits until the user presses the submit button, then it calls a function.
You need to remove your while loop, and simply respond to the event appropriately in your button callback, or in a function called by your callback.

Categories