I made this GUI:
And after I click 'Run', my main program starts.
In my main program, I have a lot of prints that I want to display in the GUI, but have no idea how.
I saw some examples in google but it was really hard to understand and how to convert it to my needs, so: How to make a window with all the prints after I click the Run button ?
code:
from tkinter import *
from main import *
root = Tk()
root.configure(background="orange")
root.wm_title("Python Project")
label_1 = Label(root, text="Project Name",bg="orange",fg="black")
label_2 = Label(root, text="Site URL Link",bg="orange",fg="black")
entry_1 = Entry(root)
entry_2 = Entry(root)
label_1.grid(row=0,sticky=W)
label_2.grid(row=3333,sticky=W)
entry_1.grid(row=0, column=1, padx=50, ipadx=100)
entry_2.grid(row=3333, column=1, ipadx=100)
def callback():
a1 = entry_1.get()
a2 = entry_2.get()
mmm(a1,a2) # main program
button1 = Button(root,text="Run",command=callback)
button2 = Button(root,text="Quit",command=root.quit)
button1.grid(row=3334, ipadx=15, padx=50, column=1)
button2.grid(row=3335, column=1, ipadx=15, padx=50)
root.mainloop()
Related
(Edit: Final working code below!)
I have been trying to do a tkinter text box with three inputs at the beginning of a code.
My main objective is to retrieve these three inputs as strings, which I would be able to integrate with the rest of the code (anything that goes beyond the root.mainloop()).
I am able to to the tkinter basics, but I am unable to integrate the user response with the other part of the code.
import tkinter as tk
from tkinter import ttk
def close_window():
root.destroy()
def show_entry_fields():
print("First %s\nSecond %s\nThird %s" % (e1.get(), e2.get(), e3.get()))
firstinput = e1.get()
secondinput = e2.get()
thirdinput = e3.get()
root = tk.Tk()
tk.Label(root, text="First input").grid(row=0)
tk.Label(root, text="Second input").grid(row=1)
tk.Label(root, text="Third input").grid(row=2)
text1 = tk.StringVar()
text2 = tk.StringVar()
text3 = tk.StringVar()
e1 = tk.Entry(root, textvariable=text1)
e1.grid(row=0, column=1)
e2 = tk.Entry(root, textvariable=text2)
e2.grid(row=1, column=1)
e3 = tk.Entry(root, textvariable=text3)
e3.grid(row=2, column=1)
tk.Button(root, text='Enter', command=show_entry_fields).grid(row=3,
column=1,
sticky=tk.W,
pady=4)
tk.Button(root, text='Close', command=close_window).grid(row=4,
column=1,
sticky=tk.W,
pady=4)
root.mainloop()
firstinput
print(firstinput)
In that sense, my objective is to be able to use the firstinput, secondinput and thirdinput as the code goes on beyond the root.mainloop().
However, when I run this code, I get the following error:
NameError Traceback (most recent call last)
<ipython-input-1-76b7cc4daea7> in <module>
42 root.mainloop()
43
---> 44 firstinput
NameError: name 'firstinput' is not defined
If I can't callback upon those strings I defined in def show_entry_fields(), how can I use them in the remaining of the code?
I'm sorry if this sounded confusing, I'll be happy to better/further explain myself if necessary.
If someone can shed a light upon this, I'll be ever so grateful. Many thanks.
EDIT: With the help of the comments, here is the final (and working) version:
import tkinter as tk
from tkinter import ttk
def close_window():
root.destroy()
def show_entry_fields():
print("First %s\nSecond %s\nThird %s" % (e1.get(), e2.get(), e3.get()))
root = tk.Tk()
tk.Label(root, text="First input").grid(row=0)
tk.Label(root, text="Second input").grid(row=1)
tk.Label(root, text="Third input").grid(row=2)
text1 = tk.StringVar()
text2 = tk.StringVar()
text3 = tk.StringVar()
e1 = tk.Entry(root, textvariable=text1)
e1.grid(row=0, column=1)
e2 = tk.Entry(root, textvariable=text2)
e2.grid(row=1, column=1)
e3 = tk.Entry(root, textvariable=text3)
e3.grid(row=2, column=1)
tk.Button(root, text='Enter', command=show_entry_fields).grid(row=3,
column=1,
sticky=tk.W,
pady=4)
tk.Button(root, text='Close', command=close_window).grid(row=4,
column=1,
sticky=tk.W,
pady=4)
root.mainloop()
firstinput = text1.get()
secondinput = text2.get()
thirdinput = text3.get()
print(firstinput)
from tkinter import *
import os
os.system ("clear")
root = Tk()
root.title('Test')
root.geometry('1000x600')
def submit():
submit_text = Label(text=(eq))
submit_text.pack()
label = Label(root, text='Enter the first mounth')
label.pack()
textbox3 = Entry(root, width=30)
textbox3.pack()
textbox2 = Entry(root, width=30)
textbox2.pack()
textbox1 = Entry(root, width=30)
textbox1.pack()
eq = textbox1.get() + textbox2.get() + textbox3.get()
button = Button(root, text="submit", command=submit)
button.pack()
root.mainloop()
I want the code to open up a window and I'm going to put 3 numbers and I want the code to add the 3 numbers and show me the result when II press the button, but it does nothing.
I want to take user input and output it inside GUI ...
my code
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label = Label(root, text="???")
label.grid(row=1, column=2)
ok=Label(root, text="Type your name").grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
root.mainloop()
To get the text from an input box, use inputbox.get(). Also, don't set the ok variable to Label(root, text="Type your name").grid(row=2,column=1). This will be set as NoneType, so do
ok = Label(root, text="Type your name").grid(row=2,column=1)
ok.grid(row=2, column=1)
Here is your code:
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label = Label(root, text=entry.get())
label.grid(row=1, column=2)
ok=Label(root, text="Type your name")
ok.grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
root.mainloop()
First thing, In order to accept and display the output on the screen you have to use either Label widget or Canvas Text. Since your code is not updating the Label widget thus I am here doing what you want to do.
First, create a Label widget in the main window,
Get the user input by using.get() method,
print and display the user input.
from tkinter import *
root = Tk()
root.geometry("644x344")
def printSomething():
label.config(text=entry.get())
ok=Label(root, text="Type your name")
ok.grid(row=2,column=1)
entryvalue = StringVar()
entry= Entry(root, textvariable=entryvalue)
entry.grid(row=2, column=2)
button = Button(root, text="Print Me", command=printSomething)
button.grid(row=3, column=2)
#Create a Label to print the Name
label= Label(root, text="", font= ('Helvetica 14 bold'), foreground= "red3")
label.grid(row=1, column=2)
root.mainloop()
So basically I am practising using Tkinter and I was going to make a little clicker game based on anime. I have researched loads, however, no matter what I try the text saying how many RC cells I have won't show anything at all. please keep it simple as I'm still new to coding.
from tkinter import *
rcc=1
x=1
#click function
def click():
global rcc
rcc=rcc+1
return rcc
while x==1:
rccc=str(rcc)
window=Tk()
window.title("my click game boi")
rc=StringVar()
rc.set=(rccc)
window.configure(background="black")
##### my photo
photo1=PhotoImage(file="kanekipic.gif")
Label (window, image=photo1, bg='black') .grid(row=0, column=0, sticky=E)
Button(window, text="eat", width=3, command=click) .grid(row=3, column=0,
sticky=W)
Label1=Label(window, textvariable=rc, bg="red", fg="white") .grid(row=1, column=0, sticky=S)
print (rcc)
window.mainloop()
The best solution (in the sense that it preserves the desired goal and is "kept simple"), I came up with is:
#!/usr/bin/env python3
from tkinter import *
rcc = 1
def click():
global rcc
rcc += 1
rc.set(rcc)
print('rcc was increased to {}'.format(rcc))
if __name__ == '__main__':
window = Tk()
window.title("my click game boi")
window.configure(background='black')
rc = IntVar()
photo1 = PhotoImage(file="kanekipic.gif")
Label(window, image=photo1, bg='black').grid(row=0, column=0, sticky=E)
Button(window, text="eat", width=3, command=click).grid(row=3, column=0, sticky=W)
Label(window, textvariable=rc, bg="red", fg="white").grid(row=1, column=0, sticky=S)
window.mainloop()
Whenever you click on the eat button, the counter will be increased.
Full disclosure, I am a Python newb, and even newer to Tkinter. I have the below code which I was able to parse together from various sources which shows the buttons and dialogue box as I want them to be shown. The "Cancel" button works appropriately, but when I enter proper credentials and click "OK", nothing happens. Based on what I have read, I think I may have a binding and/or callback issue, but I am not sure. After several hours of reading and watching YouTube videos, I am banging my head against the desk. Any help would be greatly appreciated.
from Tkinter import *
master = Tk()
def login_info():
bankUsername = bank_user.get()
bankPassword = bank_pass.get()
return
Label(master, text=str(properBankName) + " Username: ").grid(row=0, sticky = "E")
Label(master, text="Password: ").grid(row=1, sticky = "E")
master.title("Please Enter Credentials")
bank_user = Entry(master)
bank_pass = Entry(master)
bank_user.grid(row=0, column=1)
bank_pass.grid(row=1, column=1)
Button(master, height=1, width=8, text='OK', command=login_info).grid(row=3, column=0, sticky = "E", pady=4)
Button(master, height=1, width=8, text='Cancel', command=master.quit).grid(row=3, column=1, sticky = "W", pady=4)
master.mainloop()
I use global variables to keep values and then I use master.destroy() to close window.
(on Linux master.quit() doesn't execute master.destroy() which close window)
I use variable login to recognize which button was clicked.
I use columnspan=2 for Entry and move Buttons one cell to the right - and now it looks better.
BTW: lines Button(...).grid(...) were very long so I splited them into two lines to make more readable.
Code:
from Tkinter import *
# --- functions ---
def login_info():
# inform function to use external/global variables
global bankUsername
global bankPassword
global login
login = True
bankUsername = bank_user.get()
bankPassword = bank_pass.get()
# quit window
master.destroy()
# --- main ---
# create global variables
bankUsername = None
bankPassword = None
login = False
# - GUI
master = Tk()
Label(master, text="Username:").grid(row=0, sticky="E")
Label(master, text="Password:").grid(row=1, sticky="E")
master.title("Please Enter Credentials")
bank_user = Entry(master)
bank_pass = Entry(master)
bank_user.grid(row=0, column=1, columnspan=2)
bank_pass.grid(row=1, column=1, columnspan=2)
b = Button(master, height=1, width=8, text='OK', command=login_info)
b.grid(row=3, column=1, sticky="E", pady=4)
b = Button(master, height=1, width=8, text='Cancel', command=master.destroy)
b.grid(row=3, column=2, sticky="W", pady=4)
master.mainloop()
# --- executed after closing window ---
if login: # if login is True:
print(bankUsername)
print(bankPassword)
else:
print("Canceled")