I am using Idle and tkinter.messagebox. I am trying to display the product of a calculation from entry inputs. I have been unable to figure out how to capture the user entries and then run the calculation and produce the result.
So, what I want the user to be able to input numbers, say 2, and 2. Then, I want the user to push a button. Next, the answer to 2+2 should appear in the blank entry label.
My question is how do I capture user inputs and use them in a calculation, then display the result?
mydict = {'good':0.7, 'average':0.5, 'optimal': 1, 'unacceptable': 0, 'major innovation': 1, 'minor innovation': 0.7, 'no innovation': 0.4, '1st lowest': 1, '2nd lowest': 0.7, '3rd lowest': 0.5, '4th lowest': 0.3}
# Widgets:
from tkinter import *
master = Tk()
label1 = Label(master, text = 'Bid Number', relief = 'groove', width = 16)
label2 = Label(master, text = 'Cost Score', relief = 'groove', width = 16)
label3 = Label(master, text = 'Past Performance', relief = 'groove', width = 16)
label8 = Label(master, text = 'Bid 1' , relief = 'groove', width = 12)
entry1 = Entry(master, relief = 'groove', width = 12)
entry2 = Entry(master, relief = 'groove', width = 12)
label9 = Label(master, text = 'Bid Score', relief = 'groove', width = 16)
def button_function():
a = float( mydict[entry1.get()] ) * float( mydict[entry2.get()] )
button1 = Button(master, text = 'calculate', relief = 'groove', width = 12, command = button_function)
label16 = Frame(master, print(a))
#Geometry
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 1, column = 2, padx = 10 )
label3.grid( row = 1, column = 3, padx = 10 )
label8.grid( row = 2, column = 1, padx = 10 )
entry1.grid( row = 2, column = 2, padx = 10 )
entry2.grid( row = 2, column = 3, padx = 10 )
label9.grid( row = 1, column = 8, padx = 10 )
label16.grid( row = 2, column = 8, padx = 10 )
button1.grid( row = 3, column = 4, columnspan = 2)
I don't think this question would help anyone but you in its current state but I think what you're trying to achieve can be done by replacing:
def button_function():
a = float( mydict[get(entry1)] ) * float( mydict[get(entry1)] ) * float( mydict[get(entry1)] ) *float( mydict[get(entry2)] ) * float( mydict[get(entry3)] ) *float( mydict[get(entry4)] ) * float( mydict[get(entry5)] ) * float( mydict[get(entry5)] ) * float( mydict[get(entry6)])
...
with:
def button_function():
a = float( mydict[entry1.get()] ) * float( mydict[entry1.get()] )\
* float( mydict[entry1.get()] ) *float( mydict[entry2.get()] )\
* float( mydict[entry3.get()] ) *float( mydict[entry4.get()] )\
* float( mydict[entry5.get()] ) * float( mydict[entry5.get()] )\
* float( mydict[entry6.get()])
...
Additionally, add:
label16.grid()
button1.grid()
in order to display, not shown but obviously required to be shown widgets.
I solved the problem. I needed to define show_answer and insert it into the blank, then command the button to show answer. Thanks for the help.
mydict = {'good':0.7, 'average':0.5, 'optimal': 1, 'unacceptable': 0, 'major innovation': 1, 'minor innovation': 0.7, 'no innovation': 0.4, '1st lowest': 1, '2nd lowest': 0.7, '3rd lowest': 0.5, '4th lowest': 0.3}
# Widgets:
from tkinter import
*from tkinter.messagebox import *
master = Tk()
label1 = Label(master, text = 'Bid Number', relief = 'groove', width = 16)
label2 = Label(master, text = 'Cost Score', relief = 'groove', width = 16)
label3 = Label(master, text = 'Past Performance', relief = 'groove', width = 16)
label8 = Label(master, text = 'Bid 1' , relief = 'groove', width = 12)
entry1 = Entry(master, relief = 'groove', width = 12)
entry2 = Entry(master, relief = 'groove', width = 12)
label9 = Label(master, text = 'Bid Score', relief = 'groove', width = 16)
blank1 = Entry(master, relief = 'groove', width = 16)
def button_function():
float( mydict[entry1.get()] ) * float( mydict[entry2.get()] )
def show_answer():
Ans = float( mydict[entry1.get()] ) * float( mydict[entry2.get()] )
blank1.insert(0, Ans)
button1 = Button(master, text = 'calculate', relief = 'groove', width = 12, command =show_answer)
#Geometry
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 1, column = 2, padx = 10 )
label3.grid( row = 1, column = 3, padx = 10 )
label8.grid( row = 2, column = 1, padx = 10 )
entry1.grid( row = 2, column = 2, padx = 10 )
entry2.grid( row = 2, column = 3, padx = 10 )
label9.grid( row = 1, column = 8, padx = 10 )
blank1.grid( row = 2, column = 8, padx = 10 )
button1.grid( row = 3, column = 4, columnspan = 2)
Related
I am new to Python and am using Tkinter to create a basic GUI for a game.
I have got some buttons an image and numerous labels to work, but I am wondering..
What is the best way to get a large, initially blank text box on the GUI, and how would I go about having the text box update on the press of a "next" button.
I need it to scroll through text with each press, showing the next relevant line.
The text that is shown will depend on the choice(s) that are made, so variables like current location etc are necessary.
The aim is to make a simple game that allows you to read through the text with the Next button, then make choices etc, a mostly text-based adventure game.
I'll post what I have already below. Yes I know it's terrible, this is my first try so don't judge too harshly on my mistakes.
Any tips, advice or methods are much appreciated.
Some things to note:
My quit button isn't working yet, not too sure why.
I would love to figure out how to store variables based on what the user inputs, via an input box that can be prompted when an input is required from the user.
Also, happy to start from scratch if it's easier.
# Widgets:
import sys #Imports the system commands which makes it possible to terminate the program
import time #Imports the time module to allow delays in script
import os #Imports os to make it possible to play music/sound
import random #Imports random variable to allow for random number generation
import pickle #allows the game state to be saved.
from tkinter import * # imports tkinter
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from textwrap import wrap # imports text wrap
#base variables for testing
character_Gold = 1
character_Health = 100
character_base_Health = 100
character_Strength = 10
character_Dexterity = 10
character_Constitution = 10
character_Intelligence = 10
character_Wisdom = 10
character_Charisma = 10
character_base_Perception = 10
#Text Boxes
#Base Geometry and image
window = Tk()
TitleScreen = PhotoImage( file = 'test.gif' )
window.geometry("1100x600")
#defines
def close():
#win.destroy()
window.quit()
imgLbl = Label( window, image = TitleScreen )
label1 = Label( window, relief = 'groove', width = 4)
label2 = Label( window, relief = 'groove', width = 4)
label3 = Label( window, relief = 'groove', width = 4)
label4 = Label( window, relief = 'groove', width = 4)
label5 = Label( window, relief = 'groove', width = 4)
label6 = Label( window, relief = 'groove', width = 4)
label7 = Label( window, relief = 'groove', width = 4)
label8 = Label( window, relief = 'groove', width = 4)
label9 = Label( window, relief = 'groove' , width = 3)
label10 = Label( window, relief = 'groove' , width = 3)
label11 = Label( window, relief = 'groove' , width = 3)
label12 = Label( window, relief = 'groove' , width = 3)
label13 = Label( window, relief = 'groove' , width = 3)
label14 = Label( window, relief = 'groove' , width = 3)
label15 = Label( window, relief = 'groove' , width = 3)
label16 = Label( window, relief = 'groove' , width = 3)
TextLabel = Label( window, relief = 'groove', width = 50)
LocationLabel = Label( window, relief = 'groove', width = 18)
LocationBase = Label( window, relief = 'groove', width = 13)
NextBtn = Button( window )
MainText = Label( window, relief = 'groove', width = 40, height = 8)
QuitButton = Button(window, text="Quit",command=close)
# Geometry:
imgLbl.grid( row = 1, column = 1 , rowspan = 24 ) # Change Rowspan here to increase number of Rows and shrink gaps
label1.grid( row = 1, column = 2, padx = 10 )
label2.grid( row = 1, column = 4, padx = 10 )
label3.grid( row = 1, column = 5, padx = 10 )
label4.grid( row = 1, column = 6, padx = 10 )
label5.grid( row = 1, column = 7, padx = 10 )
label6.grid( row = 1, column = 8, padx = 10)
label7.grid( row = 1, column = 9, padx = 10)
label8.grid( row = 1, column = 10, padx = 5)
label9.grid( row = 2, column = 2, padx = (1, 10)) #Health
label10.grid(row = 2, column = 4, padx = 10) #STR
label11.grid(row = 2, column = 5, padx = 10) # DEX
label12.grid(row = 2, column = 6, padx = 10) # CON
label13.grid(row = 2, column = 7, padx = 10) #INT
label14.grid(row = 2, column = 8, padx = 10) #WIS
label15.grid(row = 2, column = 9, padx = 10) # CHA
label16.grid(row = 2, column = 10, padx= 10) # gold
TextLabel.grid( row = 26, column = 1, padx = 10) # instruction label
LocationLabel.grid( row = 2, column = 11, padx = 10) # dynamic location label, changes
LocationBase.grid( row = 1, column = 11, padx = 10) # location text only not dynamic
MainText.grid( row = 30, column = 1, padx = 10) # main text box
QuitButton.grid(row = 34, column = 14 , padx = 10) #Quit Button
NextBtn.grid( row = 26, column = 2, columnspan = 4 )
# Static Properties
window.title( ' The Adventure ')
NextBtn.configure( text = 'Next')
Current_Location = "Title Screen"
#DynamicProperties. BD is button facelift level, bg is colour.
label1.configure( text = 'HP:', bg = 'Red' )
label1.configure( bd = 8, relief=RAISED)
label2.configure( text = 'STR:', bg = 'Orange')
label2.configure( bd = 8, relief=RAISED)
label3.configure( text = 'DEX:', bg = 'Purple')
label3.configure( bd = 8, relief=RAISED)
label4.configure( text = 'CON:', bg = 'Green')
label4.configure( bd = 8, relief=RAISED)
label5.configure( text = 'INT:', bg = 'light green')
label5.configure( bd = 8, relief=RAISED)
label6.configure( text = 'WIS:', bg = 'white')
label6.configure( bd = 8, relief=RAISED)
label7.configure( text = 'CHA:', bg = 'light blue')
label7.configure( bd = 8, relief=RAISED)
label8.configure( text = 'GP:', bg = 'yellow')
label8.configure( bd = 8, relief=RAISED)
label9.configure( text = character_Health)
label9.configure( bd = 6, relief=RAISED)
label10.configure( text = character_Strength)
label10.configure( bd = 6, relief=RAISED)
label11.configure( text = character_Dexterity)
label11.configure( bd = 6, relief=RAISED)
label12.configure( text = character_Constitution)
label12.configure( bd = 6, relief=RAISED)
label13.configure( text = character_Intelligence)
label13.configure( bd = 6, relief=RAISED)
label14.configure( text = character_Wisdom)
label14.configure( bd = 6, relief=RAISED)
label15.configure( text = character_Charisma)
label15.configure( bd = 6, relief=RAISED)
label16.configure( text = character_Gold)
LocationLabel.configure( text = Current_Location, fg = 'White', bg = 'Black')
LocationLabel.configure( bd = 8, relief=RAISED) # raises the button up to look 3d
LocationBase.configure( text = "Your Location:", fg = "black", bg = "White") #"location" text above tab that changes actual location
LocationBase.configure( bd = 8, relief=RAISED)
TextLabel.configure( text = "Welcome To The Adventure. Click 'Next' to begin!") # instruction label
TextLabel.configure( bd = 2, relief=RAISED)
MainText.configure( text = '''This is the large box where \n the main game text should go.''', fg = "Black", bg = "White") # use \n for a NEWLINE
MainText.configure( bd = 10, relief=RAISED)
NextBtn.configure( bd = 8, relief=RAISED) # raises next button
QuitButton.configure( bd = 2, relief=RAISED, text="Quit",command=close)
#Sustain Window:
window.mainloop()
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')
The below program was working fine few minutes back. I made a change,ran the code, made a small mistake, Spyder crashed and now it either cannot find Frame or Groove or something else. At the moment it says GROOVE not defined.
Tried writing it in lower case and with quotes. When I do with quotes it says: TclError: bad relief "Groove": must be flat, groove, raised, ridge, solid, or sunken. When I do lower or upper case without quotes says groove not defined.
from RiskFactorConversion import *
from tkinter import ttk, StringVar, Frame, Label, Tk, Entry
mainwindow = Tk()
mainwindow.title("Risk Factor Conversion")
datatype = StringVar()
dataconvention = StringVar()
mdlname = StringVar()
instancevalue = StringVar()
axisvalue = StringVar()
def g():
datatype = e1.get()
dataconvention = e2.get()
mdlname = e3.get()
instancevalue = e4.get()
r1 = rates.srtqualifier(mdlname,datatype,dataconvention,instancevalue)
l5["text"] =r1.makequalifier()
def f():
datatype = e5.get()
dataconvention = e6.get()
axisvalue = e8.get()
fx1 = fx.felixfxvol(datatype,dataconvention,axisvalue)
l11["text"] =fx1.fxvol()
def h():
datatype = en1.get()
dataconvention = en2.get()
fx2 = fx.murexfx(datatype,dataconvention)
la4["text"] =fx2.makequalifier()
#########Felix Frame####################################
frame1 = Frame(bg="white", colormap="new", padx = 10, relief=GROOVE, borderwidth=2)
frame1.grid(row = 0, column = 0)
l0 = Label(frame1, text= "FELIX Rates", pady =5, font = ('Georgia',14,'bold'), bg="white")
l0.grid(row = 0, column = 0,sticky= W )
l1 = Label(frame1, text= "Please provide Data Type:",bg="white", justify = "right",pady =5 )
l1.grid(row = 1, column = 0, sticky= E )
e1 = Entry(frame1,bd = 2, width =50, textvariable = datatype )
e1.grid(row = 1, column = 1)
e1.focus_set()
l2 = Label(frame1,bg="white", text= "Please provide Data Convention:",justify = "right", pady = 5)
l2.grid(row = 2, column = 0,sticky= E)
e2 = Entry(frame1,bd = 2, width =50, textvariable = dataconvention )
e2.grid(row = 2, column = 1)
l3 = Label(frame1,bg="white", text= "Please provide Model Type:", justify = "right",pady = 5)
l3.grid(row = 3, column = 0,sticky= E)
e3 = ttk.Combobox(frame1,width =45, textvariable = mdlname, state='readonly')
e3['values'] = ('IR_SABR','FX_LN','IR_LGM','IR_LMM','IR_SABR_FX_LN_GC','IR_SABR_GC','INFLATION_SABR')
e3.grid(row = 3, column = 1)
l4 = Label(frame1,bg="white", text= "Please provide Instance Name:", justify = "right",pady = 5)
l4.grid(row = 4, column = 0,sticky= E)
e4 = Entry(frame1,bd = 2, width =50, textvariable = instancevalue )
e4.grid(row = 4, column = 1)
bfelix = Button(frame1, text = "Press to get Qualifier Value", pady = 10, command = g)
bfelix.grid(row = 5, column = 0)
l5 = Label(frame1,bg="white", text= "" , justify = "right",pady = 5)
l5.grid(row = 5, column = 1)
################################################################
############FELIX FX############################################
frame2 = Frame(bg="white", colormap="new", padx = 10, relief=GROOVE, borderwidth=2)
frame2.grid(row = 0, column = 1)
l6 = Label(frame2, text= "FELIX FX", pady =5, font = ('Georgia',14,'bold'), bg="white")
l6.grid(row = 0, column = 0,sticky= W )
l7 = Label(frame2, text= "Please provide Data Type:",bg="white", justify = "right",pady =5 )
l7.grid(row = 1, column = 0, sticky= E )
e5 = Entry(frame2,bd = 2, width =50, textvariable = datatype)
e5.grid(row = 1, column = 1)
e5.focus_set()
l8 = Label(frame2,bg="white", text= "Please provide Data Convention:",justify = "right", pady = 5)
l8.grid(row = 2, column = 0,sticky= E)
e6 = Entry(frame2,bd = 2, width =50, textvariable = dataconvention )
e6.grid(row = 2, column = 1)
l10 = Label(frame2,bg="white", text= "Please provide axis 2 value:", justify = "right",pady = 5)
l10.grid(row = 3, column = 0,sticky= E)
e8 = Entry(frame2,bd = 2, width =50, textvariable = axisvalue )
e8.grid(row = 3, column = 1)
bfelixfx = Button(frame2, text = "Press to get Qualifier Value", pady = 10, command = f)
bfelixfx.grid(row = 4, column = 0)
l11 = Label(frame2,bg="white", text= "" , justify = "right",pady = 5)
l11.grid(row = 4, column = 1)
#####################################################################
############MUREX FX############################################
frame3 = Frame(bg="white", colormap="new", padx = 10, relief=GROOVE, borderwidth=2)
frame3.grid(row = 1, column = 1)
la = Label(frame3, text= "Murex FX", pady =5, font = ('Georgia',14,'bold'), bg="white")
la.grid(row = 0, column = 0,sticky= W )
la1 = Label(frame3, text= "Please provide Risk Factor Type:",bg="white", justify = "right",pady =5 )
la1.grid(row = 1, column = 0, sticky= E )
en1 = ttk.Combobox(frame3, width =45, textvariable = mdlname, state='readonly')
en1['values'] =('FX ATM VOLATILITY','FX BUTTERFLY 10D','FX BUTTERFLY 25D','FX RISK REVERSAL 10D','FX RISK REVERSAL 25D')
en1.grid(row = 1, column = 1)
en1.focus_set()
la2 = Label(frame3,bg="white", text= "Please provide Currency Pair:",justify = "right", pady = 5)
la2.grid(row = 2, column = 0,sticky= E)
en2 = Entry(frame3,bd = 2, width =50, textvariable = dataconvention )
en2.grid(row = 2, column = 1)
bmurexfx = Button(frame3, text = "Press to get Qualifier Value", pady = 10, command = h)
bmurexfx.grid(row = 4, column = 0)
la4 = Label(frame3,bg="white", text= "" , justify = "right",pady = 5)
la4.grid(row = 4, column = 1)
#####################################################################
mainwindow.mainloop()
Groove is a constant defined in Tkinter and since you are only importing specfic functions from Tkinter that doesn't include Groove, you need to add GROOVE to it or add
import tkinter as tk
and then set relief=tk.GROOVE
I created a loop to search a CSV file and return a row with a specified keyword. When the else function is hastagged out, the loop works fine.
import csv
from tkinter import *
import tkinter.messagebox as box
master = Tk()
label1 = Label(master, text = 'User_ID', relief = 'groove', width = 40)
label2 = Label(master, text = 'User_Info', relief = 'groove', width = 40, height = 5)
e1 = Entry(master, relief = 'groove', width = 40)
e2 = Text(master, relief = 'groove', width = 40, height = 5, borderwidth = 2)
def enter():
csvfile = open('stack_example.csv', 'r')
read = csv.reader(csvfile)
for row in read:
if str(e1.get()) in row:
e2.insert("1.0", row, 'r')
#break
#else:
#box.showinfo('Search Result','Not Found')
#master.mainloop()
button3 = Button(master, text = 'Retrieve File', relief = 'groove', width = 25, command=enter)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 2, column = 1, padx = 10 )
e1.grid( row = 1, column = 2, padx = 10 )
e2.grid( row = 2, column = 2, padx = 10 )
button3.grid( row = 3, column = 1, columnspan = 2)
However, when the else function is part of the code, the loop skips right to the else command and ignores the if command. The output produced above is an insertion of the row with the keyword from the csv file to a text widget. But, the output below is a display box. (with the same entry keyword, that is in the csv file)
import csv
from tkinter import *
import tkinter.messagebox as box
master = Tk()
label1 = Label(master, text = 'User_ID', relief = 'groove', width = 40)
label2 = Label(master, text = 'User_Info', relief = 'groove', width = 40, height = 5)
e1 = Entry(master, relief = 'groove', width = 40)
e2 = Text(master, relief = 'groove', width = 40, height = 5, borderwidth = 2)
def enter():
csvfile = open('stack_example.csv', 'r')
read = csv.reader(csvfile)
for row in read:
if str(e1.get()) in row:
e2.insert("1.0", row, 'r')
#break
else:
box.showinfo('Search Result','Not Found')
master.mainloop()
button3 = Button(master, text = 'Retrieve File', relief = 'groove', width = 25, command=enter)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 2, column = 1, padx = 10 )
e1.grid( row = 1, column = 2, padx = 10 )
e2.grid( row = 2, column = 2, padx = 10 )
button3.grid( row = 3, column = 1, columnspan = 2)
My understanding is that only if an if statement is false does the else command execute. However, the else statement seems to be overriding the if statement with this code. I am unsure of how to write a loop that will function so that:
if the keyword is in the csvfile
then the row with the keyword is inserted to a blank widget
if the keyword is not in the csvfile
then a box with text 'Not Found' is displayed
When your else condition is observed at least once in the for loop, the text will change. So, your if statement code is likely still being evaluated, but you are also observing the else behavior in the same loop. Try this:
import csv
from tkinter import *
import tkinter.messagebox as box
master = Tk()
label1 = Label(master, text = 'User_ID', relief = 'groove', width = 40)
label2 = Label(master, text = 'User_Info', relief = 'groove', width = 40, height = 5)
e1 = Entry(master, relief = 'groove', width = 40)
e2 = Text(master, relief = 'groove', width = 40, height = 5, borderwidth = 2)
def enter():
csvfile = open('stack_example.csv', 'r')
read = csv.reader(csvfile)
contains_keyword = False
for row in read:
if str(e1.get()) in row:
contains_keyword = True
break
if contains_keyword:
e2.insert("1.0", row, 'r')
else:
box.showinfo('Search Result','Not Found')
master.mainloop()
button3 = Button(master, text = 'Retrieve File', relief = 'groove', width = 25, command=enter)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 2, column = 1, padx = 10 )
e1.grid( row = 1, column = 2, padx = 10 )
e2.grid( row = 2, column = 2, padx = 10 )
button3.grid( row = 3, column = 1, columnspan = 2)
Thank you for reading. I am new to coding and have googled this problem and consulted text resources without success. I do not have access to a teacher. I did my best to create a version of the problem in as simple a way as I could. I greatly appreciate your time and effort in helping me to solve this problem.
I am working in Python using IDLE and tkinter.
I am trying to link two interfaces using tkinter. I designed the first interface to ask for two entries, (defined as factor1 and factor2). I also put a button on the first interface. I am trying to create a command for the button to open the second interface.
Once, the second interface is open, the second interface is designed to use the user inputs into the two entries from the first interface (defined as factor1 and factor2) as labels.
#First Interface
from tkinter import *
from tkinter.messagebox import *
master = Tk()
master.title('Getting Started')
label1 = Label(master, text = 'what is factor 1:', relief = 'groove', width = 19)
label2 = Label(master, text = 'what is factor 2:', relief = 'groove', width = 19)
factor1 = Entry(master, relief = 'groove', width = 12)
factor2 = Entry(master, relief = 'groove', width = 12)
button2 = Button(master, text = 'Go', relief = 'groove', width = 25)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 2, column = 1, padx = 10 )
factor1.grid( row = 1, column = 2, padx = 10 )
factor2.grid( row = 2, column = 2, padx = 10 )
button2.grid( row = 3, column = 1, columnspan = 2)
#Second Interface
from tkinter import *
from tkinter.messagebox import *
mydict = {'good':0.75, 'outstanding': 1}
master = Tk()
label1 = Label(master, text = 'proposal', relief = 'groove', width = 19)
label2 = Label(master, text = factor1, relief = 'groove', width = 19)
label3 = Label(master, text = factor2, relief = 'groove', width = 19)
label4 = Label(master, text = 'Proposal Score', relief = 'groove', width = 19)
label5 = Label(master, text = '1', relief = 'groove', width = 12)
entry1 = Entry(master, relief = 'groove', width = 12)
entry2 = Entry(master, relief = 'groove', width = 12)
blank1 = Entry(master, relief = 'groove', width = 12)
def show_answer():
c = float( mydict[entry1.get()]) *float( mydict[entry2.get()])
blank1.insert(0, c)
button1 = Button(master, text = 'Calculate Proposal Scores', relief = 'groove', width = 25, command = show_answer)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 1, column = 2, padx = 10 )
label3.grid( row = 1, column = 3, padx = 10 )
label4.grid( row = 1, column = 4, padx = 10 )
label5.grid( row = 2, column = 1, padx = 10 )
entry1.grid( row = 2, column = 2, padx = 10 )
entry2.grid( row = 2, column = 3, padx = 10 )
blank1.grid( row = 2, column = 4, padx = 10 )
button1.grid( row = 3, column = 2, columnspan = 2)
Right now, both interfaces are in one IDLE document. However, I am unsure whether they should be kept in separate files.
My questions are:
Should I use one IDLE file or two?
If one, how do I make two separate interfaces in one idle file?
If two, how do I link the information, so that the second interface can use the inputs entered in the first interface?
How do I create a command so that the button is able to signal to close interface one and open interface two?
I understand this question may be complicated. If you have any additional books or resources to recommend I would greatly appreciate it. Additionally, if you have any suggestions on how I could make this question better, I would greatly appreciate it. Thank you!
Whether you use one file or two is completely up to you. If the code was very long, I'd recommend splitting into two files, but it's short enough that there's nothing wrong with keeping it all in the same file.
What you should do, however, is to separate the code into two functions. One function will open the first window and ask the user to input the two factors. Then when the button is pressed, it will take the two factors and use them as arguments for the second function (which opens the 2nd window). The code looks like this:
from tkinter import *
from tkinter.messagebox import *
def first_interface():
master = Tk()
master.title('Getting Started')
label1 = Label(master, text = 'what is factor 1:', relief = 'groove', width = 19)
label2 = Label(master, text = 'what is factor 2:', relief = 'groove', width = 19)
factor1 = Entry(master, relief = 'groove', width = 12)
factor2 = Entry(master, relief = 'groove', width = 12)
def start_second_interface():
f1 = float(factor1.get())
f2 = float(factor2.get())
second_interface(f1, f2)
button2 = Button(master, text = 'Go', relief = 'groove', width = 25, command=start_second_interface)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 2, column = 1, padx = 10 )
factor1.grid( row = 1, column = 2, padx = 10 )
factor2.grid( row = 2, column = 2, padx = 10 )
button2.grid( row = 3, column = 1, columnspan = 2)
def second_interface(factor1, factor2):
mydict = {'good':0.75, 'outstanding': 1}
master = Toplevel()
label1 = Label(master, text = 'proposal', relief = 'groove', width = 19)
label2 = Label(master, text = factor1, relief = 'groove', width = 19)
label3 = Label(master, text = factor2, relief = 'groove', width = 19)
label4 = Label(master, text = 'Proposal Score', relief = 'groove', width = 19)
label5 = Label(master, text = '1', relief = 'groove', width = 12)
entry1 = Entry(master, relief = 'groove', width = 12)
entry2 = Entry(master, relief = 'groove', width = 12)
blank1 = Entry(master, relief = 'groove', width = 12)
def show_answer():
c = float( mydict[entry1.get()]) *float( mydict[entry2.get()])
blank1.insert(0, c)
button1 = Button(master, text = 'Calculate Proposal Scores', relief = 'groove', width = 25, command = show_answer)
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 1, column = 2, padx = 10 )
label3.grid( row = 1, column = 3, padx = 10 )
label4.grid( row = 1, column = 4, padx = 10 )
label5.grid( row = 2, column = 1, padx = 10 )
entry1.grid( row = 2, column = 2, padx = 10 )
entry2.grid( row = 2, column = 3, padx = 10 )
blank1.grid( row = 2, column = 4, padx = 10 )
button1.grid( row = 3, column = 2, columnspan = 2)
first_interface()
This has solved all your problems in a single simple step: The code is neatly organized, and you can easily pass the two factors to the second window.
(P.S. I've used a tk.TopLevel instead of a tk.Tk for the second window. There should be exactly one tk.Tk instance in each program. If you use more than one, you're going to run into problems.)