#๐ tkinter question
60 messages ยท Page 1 of 1 (latest)
@uncut fog
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
depends on what you're talking about when you say "output field".. are you talking about a tkinter widget or in the console or what
import tkinter as tk
def myfunction():
text_widget.insert('end', 'hello world')
root = tk.Tk()
button = tk.Button(root, text='Click Me', command=lambda: myfunction())
button.pack()
text_widget = tk.Text(root)
text_widget.pack()
root.mainloop()
well yeah you have to create the widget, if that's what you want
you can do something like this too
import tkinter as tk
def myfunction():
text_widget.insert('end', entry.get())
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
button = tk.Button(root, text='Click Me', command=lambda: myfunction())
button.pack()
text_widget = tk.Text(root)
text_widget.pack()
root.mainloop()
im just not sure what you're wanting to do
im guessing
import tkinter as tk
def myfunction():
text_widget.insert('end', entry.get())
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
button = tk.Button(root, text='Click Me', command=lambda: myfunction())
button.pack()
text_widget = tk.Text(root)
text_widget.pack()
root.mainloop()
Just use the previous example i gave you.. you can change "Click Me" to say "1" if you want to
the first argument of insert is where it is to be inserted
"1" isn't a valid insert position
Peanuts' example above uses 'end'
import tkinter as tk
def myfunction():
# don't change this
text_widget.insert(end, entry.get())
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
# change this text field
button = tk.Button(root, text='1', command=lambda: myfunction())
button.pack()
text_widget = tk.Text(root)
text_widget.pack()
root.mainloop()
then it would be text_widget.insert('end', '1')
right
the entry was an example to show how to get the text from a different location, but if you only ever want "1" then no, you don't
No, there's a way to pass values into a function call from a button
no, you can just create 1 function with an argument
you can use functools.partial
if you haven't learned basic python, you probably shouldn't be messing with Tkinter yet
well then you already know how then
It's not quite so straightforward with buttons passing arguments
Do you know how to add a parameter to a function?
import tkinter as tk
def myfunction(value):
print(value)
root = tk.Tk()
button = tk.Button(root, text='1', command=lambda: myfunction(1))
button.pack()
button2 = tk.Button(root, text='2', command=lambda: myfunction(2))
button2.pack()
root.mainloop()
this works as is, but if you wanted to use a loop to avoid repetition, this would break
he's just learning the basics
just informing of the potential issue as its a very common issue people run into
i wouldn't ever use a loop to create the widgets, i'd draw them out like this instead
personal preference
I would 100% use a loop to create them
so much more control if you dont use loops
not necessarily
this is from a loop
there's no way I would type that out manually
especially if I wanted identical styling for each button
I'd much rather have 1 button created 10 times that I could edit instead of having to update 10 different buttons if I decide to change the style
I'd really recommend using Entry or Label to display the calculator output instead of Text
you absolutely can
show your full code
simply set a larger font on it
it will grow with the font
sticky is for the placement of the widget
justify is for the text placement within
justify='right'
you never added the parameter to your function
Always take a moment to think about the error before asking for help. It will train your debugging skills.
always show your code
well your parameter is value, and you're doing value()
you can't call an int, just as the error is telling you
focus on one thing at a time
just get the buttons hooked up first
then you can worry about building the equations
@olive stone when you are done would you be able to help me please?
Open a help thread and I'm sure someone will come by
ok
Yes, but you might want more of a 3x3 grid like an actual calculator
there's still some logic you'll need to create for the display, like introducing . for decimals, or making sure you can't just enter 0 over and over
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.