0

I am trying to play and learn with tkinter :

import tkinter as tk
from tkinter import *
from tkcalendar import Calendar, DateEntry

gui = Tk(className='STUDENTS')
gui.geometry("500x300")


def openNewWindow2():
    newWindow1 = Toplevel(gui)
    newWindow1.title("STUDENTS")
    newWindow1.geometry("500x300")
    Label(newWindow1, text='Select How many students?').pack()
    options2 = [
        "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
    ]
    clicked = StringVar()
    clicked.set("1")
    drop = OptionMenu(newWindow1, clicked, *options2)
    drop.pack()
    btn5 = Button(newWindow1,
                  text="GENERATE!!!",
                  command"#some function to use the values of dropdown and maxcal#")
    btn5.pack(pady=20)
    maxcal = DateEntry(newWindow1, width=12, background='darkblue',
                foreground='white', borderwidth=2)
    maxcal.pack(padx=10, pady=10)

label = Label(gui, text='test1~')
label.pack(pady=10)
btn2 = Button(gui,
              text="generator",
              command=openNewWindow2)

btn2.pack(pady=20)
gui.mainloop()

This will create a window with a dropdown menu. I want to hit the btn5 and store the values of the dropdown menu and the calendar in another window after. Is this possible?

5
  • You can store it in a variable and make it global. Commented Apr 21, 2021 at 10:39
  • @CoolCloud I am not familiar with tkinter. Can you tell my which line to store in variable? I printed everything of this and nothing showed the values I chose Commented Apr 21, 2021 at 10:43
  • I am not sure, what you want, so create a function, link it with the button and then inside the function say, var = clicked.get(),maxcal.get() and print(var) to see it. Commented Apr 21, 2021 at 10:45
  • I did it but it sends only the default value the 1 Commented Apr 21, 2021 at 10:53
  • I want to choose for example 5 in the dropdown and print 5 times "hello world" with another function. Commented Apr 21, 2021 at 10:56

1 Answer 1

1

This is pretty simple, something like this should work:

def openNewWindow2():
    newWindow1 = Toplevel(gui)
    def func():
        print('hello world'*int(clicked.get()))

    ....
    btn5 = Button(newWindow1,text="GENERATE!!!",command=func)

It is something special with python that I don't think any other programing languages offer. You can multiply a string(I guess all iterables) with an integer to repeat it that many times.

Sign up to request clarification or add additional context in comments.

7 Comments

The new func is inside the openNewWindow2() ? Alsο in the btn5 I put in the command= func() do I have to put also the clicked?
@DrGenius That was the problem, dont put func(). Check updated answer
@DrGenius clicked.get() is the number of times it should be multiplied. When that changes, the number of times multiplied will also change.
@DrGenius When you say func() it will directly call the function. But when you say func it will call the function only when you press the button.
You are amazing. Thank you a lot for your time and your help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.