Okay so I have this sign up form and there is a part where you have to enter your name, I want that name answer to be taken to the page after.
import tkinter as tk
root = tk.Tk()
root.geometry("150x50+680+350")
def FormSubmission():
global button_start
button_start.place_forget()
l1.place_forget()
root.attributes("-fullscreen", True)
frame = tk.Frame(root)
tk.Label(frame, text="First Name:").grid(row=0)
name = entry1 = tk.Entry(frame) # I want the name written here to be taken from here to the welcome text.
entry1.grid(row=0, column=1)
tk.Label(frame, text="Last Name:").grid(row=1)
e2 = tk.Entry(frame)
e2.grid(row=1, column=1)
tk.Label(frame, text="Email:").grid(row=2)
e3 = tk.Entry(frame)
e3.grid(row=2, column=1)
tk.Label(frame, text="Date of Birth:").grid(row=3)
e4 = tk.Entry(frame)
e4.grid(row=3, column=1)
frame.pack(anchor='center', expand=True)
button_next = tk.Button(frame, text = "Next", height = 2, width = 7, command =lambda: MainPage(frame))
button_next.grid(row=4, column=1)
def MainPage(frame):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1.place(x = 500, y = 10)
button_start.place_forget()
l1 = tk.Label(root, text="Welcome," , font=("Arial", 44)) #As you can see here in this line I want the entry 1 name here after welcome and the comma
button_start = tk.Button(root, text="Start", height=3, width=20, command = FormSubmission)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)
root.mainloop()
What I want to do is take the entry 1 answer and put it in the welcome text. There is an indicator on the lines I'm talking about.
Here is an example how to provide text from your widget entry1
in function FormSubmission():where you are defining your button you should pass the text you want to show in your label
button_next = tk.Button(frame, text = "Next", height = 2, width = 7, command =lambda: MainPage(frame, entry1.get()))
in funtion MainPage(frame):you should set text to your label:
def MainPage(frame, entry1):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1.place(x = 500, y = 10)
button_start.place_forget()
l1.config(text="Welcome," + entry1) #<-------
Related
I want to update getting results from an entry box in a way that when an integer enters, the equivalent rows of entry boxes appear below that. I have written the below code to make it work using a button. However, I want to make it happen automatically without a button as I entered the number, the rows update. I checked one way of doing that is using the after(). I placed after after() in the function and out of the function but it is not working.
from tkinter import *
root = Tk()
root.geometry("400x400")
n_para = IntVar()
label1 = Label(root, text="Numeric parameters")
label1.grid(row=0, column=0)
entry1 = Entry(root, textvariable=n_para)
entry1.grid(row=0, column=1)
def update():
for i in range(1, n_para.get()+1):
entryX = Entry(root)
entryX.grid(row=i+1, column=0)
entryY = Entry(root)
entryY.grid(row=i+1, column=1)
entryZ = Entry(root)
entryZ.grid(row=i+1, column=2)
root.after(100, update)
root.after(1, update)
button1 = Button(root, text="update", command=update)
button1.grid(row=1, column=0)
root.mainloop()
You should try using the <KeyRelease> event bind.
import tkinter as tk
def on_focus_out(event):
label.configure(text=inputtxt.get())
root = tk.Tk()
label = tk.Label(root)
label.pack()
inputtxt = tk.Entry()
inputtxt.pack()
root.bind("<KeyRelease>", on_focus_out)
root.mainloop()
This types the text entered in real-time.
Edited Code with OP's requirement:
from tkinter import *
root = Tk()
root.geometry("400x400")
n_para = IntVar()
label1 = Label(root, text="Numeric parameters")
label1.grid(row=0, column=0)
entry1 = Entry(root, textvariable=n_para)
entry1.grid(row=0, column=1)
def upd(event):
x = entry1.get()
if not x.isnumeric():
x = 0
for i in range(1, int(x)+1):
entryX = Entry(root)
entryX.grid(row=i+1, column=0)
entryY = Entry(root)
entryY.grid(row=i+1, column=1)
entryZ = Entry(root)
entryZ.grid(row=i+1, column=2)
# root.after(100, update)
root.bind("<KeyRelease>", upd)
# button1 = Button(root, text="update", command=update)
# button1.grid(row=1, column=0)
root.mainloop()
So I want to make this frame that I made disappear after the press of a button. Heres the code:
import tkinter as tk
root = tk.Tk()
root.geometry("150x50+680+350")
def FormSubmission():
global button_start
root.attributes("-fullscreen", True)
frame = tk.Frame(root)
tk.Label(frame, text="First Name:").grid(row=0)
entry1 = tk.Entry(frame)
entry1.grid(row=0, column=1)
tk.Label(frame, text="Last Name:").grid(row=1)
e2 = tk.Entry(frame)
e2.grid(row=1, column=1)
tk.Label(frame, text="Email:").grid(row=2)
e3 = tk.Entry(frame)
e3.grid(row=2, column=1)
tk.Label(frame, text="Date of Birth:").grid(row=3)
e4 = tk.Entry(frame)
e4.grid(row=3, column=1)
frame.pack(anchor='center', expand=True)
button_next = tk.Button(root, text = "Next", height = 2, width = 7, command = MainPage).pack()
button_start.place_forget()
def MainPage():
global FormSubmission
root.attributes("-fullscreen", True)
l1 = tk.Label(root, text = "Welcome Back," , font = ("Arial", 44)).pack()
button_start.place_forget() # You can also use `button_start.destroy()`
button_start = tk.Button(root, text="Start", height=3, width=20, command = FormSubmission)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)
root.mainloop()
So as you see I want to make the frame/function (FormSubmission) for the entry field disappear after pressing the next button after in the function (MainPage).
You can use pack_forget
Every geometry manager(pack, grid, place) has its own ..._forget
I've re-edit your snippet just for the use case you wished for.
Anyway I think you should re-design your app.
import tkinter as tk
root = tk.Tk()
root.geometry("150x50+680+350")
def FormSubmission():
global button_start
root.attributes("-fullscreen", True)
frame = tk.Frame(root)
tk.Label(frame, text="First Name:").grid(row=0)
entry1 = tk.Entry(frame)
entry1.grid(row=0, column=1)
tk.Label(frame, text="Last Name:").grid(row=1)
e2 = tk.Entry(frame)
e2.grid(row=1, column=1)
tk.Label(frame, text="Email:").grid(row=2)
e3 = tk.Entry(frame)
e3.grid(row=2, column=1)
tk.Label(frame, text="Date of Birth:").grid(row=3)
e4 = tk.Entry(frame)
e4.grid(row=3, column=1)
frame.pack(anchor='center', expand=True)
button_next = tk.Button(root, text = "Next", height = 2, width = 7, command = lambda: MainPage(frame)).pack()
button_start.place_forget()
def MainPage(frame):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1 = tk.Label(root, text = "Welcome Back," , font = ("Arial", 44)).pack()
button_start.place_forget() # You can also use `button_start.destroy()`
button_start = tk.Button(root, text="Start", height=3, width=20, command = FormSubmission)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)
root.mainloop()
When the "view" button is pressed, it should trigger the function solution(i) such that label should be displayed in the new window. The problem is that the window opens and the previous label is packed but the label which gets it's text from "i" does not gets packed, Is there any issue in passing the parameter.
Any help is appreciated.
root = Tk()
root.config(background = "#303939")
root.state('zoomed')
def pre():
with open("DoubtSolution.txt","r+") as f:
dousol = f.read()
dousol_lst = dousol.split("`")
k = 0
window = Tk()
window.config(background = "#303939")
window.state('zoomed')
predoubt = Label(window,
text="Previous Doubts",
fg="Cyan",
bg="#303939",
font="Helvetica 50 bold"
).grid(row=0, column=1)
def solution(text):
print(text)
window1 = Tk()
window1.config(background="#303939")
window1.state('zoomed')
sol = Label(window1,
text=text[:text.find("~")],
font=font.Font(size=20),
bg="#303939",
fg="Cyan")
sol.pack()
window1.mainloop()
for i in dousol_lst:
if i[-5:] == admno:
doubt = Label(window, text=i[i.find("]]")+2:i.find("}}}}")], font=font.Font(size=20), bg="#303939",
fg="Cyan")
doubt.grid(row=2+k, column=1, pady=10)
view = Button(
master=window,
text="View", font=font.Font(size=15, family="Helvetica"),
activebackground="White",
bg="Teal",
bd=0.8,
fg="White",
command = lambda k = k:solution(i)
)
view.grid(row=2+k, column=2, padx=20)
k=k+1
window.mainloop()
previous = Button(
master=root,
text="Previous Doubts", font="Helvetica 22 bold",
activebackground="White",
bg="Teal",
bd=0.8,
fg="White",
command = pre
).grid(row=4, column=3, padx=20)
root.mainloop()
I have this little issue with making functions run depending on the radiobutton selection as you can see in this code.
the purpose is to define which function to be executed when I press Calculate according to the radiobutton selection.
import tkinter as tk
master = tk.Tk()
tk.Label(master, text='Choose Color :').grid(row=0)
tk.Label(master, text='What Is The Number ? ').grid(row=2)
fdose = tk.Spinbox(master, from_ = 0, to = 60).grid(row=2, column=1)
def calculate():
#this should take my input from the spinbox and add 10 to it if I choose Yellow
#this should take my input from the spinbox and add 100 to it if I choose Green
pass
v = tk.IntVar()
pen = tk.Radiobutton(master, text = 'Yellow',variable = v, value = 1).grid(row=0, column=1)
pen = tk.Radiobutton(master,text ='Green', variable = v, value = 2).grid(row=1, column=1)
but1 = tk.Button(master, text = 'Close', width = 20, bg = 'black', fg = 'red',activebackground = 'red', activeforeground = 'black', command = master.destroy)
but1.grid(row = 5, column = 1)
but2 = tk.Button(master, text = 'Calculate', width = 20, bg = 'black', fg = 'red',activebackground = 'red', activeforeground = 'black', command = calculate)
but2.grid(row = 5, column = 0)
master.mainloop()
the function calculate retrieves the value selected in the radiobuttons, and calls the appropriate function.
import tkinter as tk
master = tk.Tk()
tk.Label(master, text='Choose Color :').grid(row=0)
tk.Label(master, text='What Is The Number ? ').grid(row=2)
fdose = tk.Spinbox(master, from_=0, to=60).grid(row=2, column=1)
def do_yellow():
print('doing the yellow thinghy')
def do_green():
print('doing the green thinghy')
def calculate():
"""retrieves the value selected in the radiobuttons, and
calls the appropriate function.
"""
[do_yellow, do_green][int(v.get())-1]()
v = tk.IntVar()
pen = tk.Radiobutton(master, text='Yellow', variable=v, value=1)
pen.grid(row=0, column=1)
pen = tk.Radiobutton(master, text='Green', variable=v, value = 2)
pen.grid(row=1, column=1)
but1 = tk.Button(master, text='Close', width=20, bg='black', fg='red', activebackground='red', activeforeground='black', command=master.destroy)
but1.grid(row=5, column=1)
but2 = tk.Button(master, text='Calculate', width=20, bg='black', fg='red', activebackground='red', activeforeground='black', command=calculate)
but2.grid(row=5, column=0)
master.mainloop()
I am building a small app to study the vocabulary of various languages (see below the code for Mandarin). I have the basic funcions which work well. Now I want to add a button in my GUI where i can remove entries (i.e. individual words) from the database, once i have mastered the word (i.e. a button in tkinter which would remove the entry). After removing, the random function in python should then only select words from the reduced database. Do you have any idea how to do this? Any help is welcome!
from tkinter import *
import random
import sys
import os
randvalue_start = random.randint(2, 592)
window = Tk()
window.title('Mandarin Vocabulary')
window.geometry('500x400')
icon = PhotoImage(file = r'C:\Users\PycharmProjects\Mandarin\HSKlogopng.600px.png')
icon2 = icon.subsample(5 ,5)
label1 = Label(window, image = icon2, anchor="ne")
import openpyxl
path = r"\Users\PycharmProjects\Mandarin\characters.xlsx"
worbook = openpyxl.load_workbook(path, read_only=True)
sheet = worbook.active
row_count = (sheet.max_row)
def english_btn1():
global randvalue_start
english = f"B{randvalue_start}"
english_value = sheet[english].value
label_eng = Label(window, text=english_value+":", width=20, height=3, font=("TkDefaultFont",15))
label_pin = Label(window, text="", font=30, width=15, height=3)
label_mand = Label(window, text="", font=30, width=15, height=4)
label_eng.grid(row=8,column=1,rowspan=2)
label_pin.grid(row=8, column=2)
label_mand.grid(row=9, column=2)
def pinying_btn2():
global randvalue_start
pinying = f"C{randvalue_start}"
mandarin = f"D{randvalue_start}"
pinying_value = sheet[pinying].value
mandarin_value = sheet[mandarin].value
combined = f"{pinying}'/'{mandarin}"
combined_value = f"{pinying_value}'/'{mandarin_value}"
label_pin = Label(window ,text=pinying_value,font=("TkDefaultFont",15), width=10, height=2)
label_mand = Label(window,text=mandarin_value,font=("TkDefaultFont",30), width=8,height=2, borderwidth=5,relief="ridge")
label_pin.grid(row=8,column=2)
label_mand.grid(row=9,column=2)
randvalue_start = random.randint(2, 592)
def reset():
os.execl(sys.executable, sys.executable, *sys.argv)
frame = LabelFrame(window,text="Input",padx=5, pady=5)
frame.grid(row=0,column=1,padx=10,pady=10)
btn2 = Button(frame,text = "Show answer", fg = "green" ,width=20 ,command=pinying_btn2)
btn3 = Button(frame,text = "clear", fg= "red", width=20 ,command=reset)
btn1 = Button(frame,text = "Next character", fg = "black" ,width=20,command=english_btn1)
words_label = Label(frame,text = "# of characters: " + str(row_count))
label1.grid(row=0,column=2, columnspan=2)
Label(window, text="", width=20, height=3, font=("TkDefaultFont", 15)).grid(row=8, column=1)
label_blk1 = Label(window, text="", font=("TkDefaultFont", 15), width=10, height=3).grid(row=8, column=2)
label_blk2 = Label(window, text="", font=("TkDefaultFont", 30), width=10, height=2, padx=1, pady=1).grid(row=9, column=2)
btn1.grid(padx=5, pady=5)
btn2.grid(padx=5, pady=5)
btn3.grid(padx=5, pady=5)
words_label.grid(padx=5,pady=5)
window.mainloop()
This is not really a question about Pyxl or Excel. The question is really: how do you generate random numbers that skip certain numbers.
from random import randint
n = 592
numbers = list(range(n))
# To get a random number from the list
random_number = numbers[randint(0, len(numbers)-1)]
# To remove 123 from the list
numbers.remove(123)