I am really frustrated, trying to align a label and entry buttons in tkinter.
I wanted to create a GUI like below
The above page developed using page tool. I written a code to get the same kind of page, but it has lot of misalignment.
from Tkinter import *
root = Tk()
root.title("Nokia Performance")
root.geometry("583x591+468+158")
root.title("NOKIA _ANSI Performance")
root.configure(borderwidth="1")
root.configure(relief="sunken")
root.configure(background="#dbd8d7")
root.configure(cursor="arrow")
root.configure(highlightbackground="#d9d9d9")
root.configure(highlightcolor="black")
Label1 = Label(root)
_img1 = PhotoImage(file="C:\\Users\\vkandhav\\Desktop\\PY_IMAGE\\NOKIA.gif")
Label1.configure(image=_img1)
Label1.configure(text='''Label''')
Label1.pack()
Label2 = Label(root)
Label2.configure(text='''Enter the platform :''')
Label2.pack(side=LEFT)
Entry2 = Entry(root)
Entry2.pack(side = RIGHT)
Label3 = Label(root)
Label3.configure(text='''Device IP Address :''')
Label3.pack()
Entry5 = Entry(root)
Entry5.pack()
Label5 = Label(root)
Label5.configure(text='''Username :''')
Label5.pack()
Label6 = Label(root)
Label6.configure(text='''Label''')
Label6.pack()
Label7 = Label(root)
Label7.configure(text='''Craft IP :''')
Label7.pack()
Label8 = Label(root)
Label8.configure(text='''GICCI IP :''')
Label8.pack()
Label9 = Label(root)
Label9.configure(text='''Password :''')
Label9.pack()
Label10 = Label(root)
Label10.configure(text='''STC File Location''')
Label10.pack()
Label11 = Label(root)
Label11.configure(text='''STC Port :''')
Label1.pack()
Label12 = Label(root)
Label12.configure(text='''STC IP :''')
Label2.pack()
Entry6 = Entry(root)
Entry6.configure(background="white")
Entry6.configure(foreground="#000000")
Entry6.pack()
Entry7 = Entry(root)
Entry7.configure(background="white")
Entry7.pack()
Entry8 = Entry(root)
Entry8.configure(background="white")
Entry8.pack()
Entry9 = Entry(root)
Entry9.configure(background="white")
Entry9.configure(foreground="#000000")
Entry9.pack()
Entry10 = Entry(root)
Entry10.configure(background="white")
Entry10.configure(foreground="#000000")
Entry10.pack()
Entry11 = Entry(root)
Entry11.configure(background="white")
Entry11.configure(foreground="#000000")
Entry11.pack()
Radiobutton1 = Radiobutton(root)
Radiobutton1.configure(justify=LEFT)
Radiobutton1.configure(text='''NRNT-A''')
Radiobutton1.pack()
Radiobutton2 = Radiobutton(root)
Radiobutton2.configure(justify=LEFT)
Radiobutton2.configure(text='''SX-12VP''')
Radiobutton2.pack()
Radiobutton3 = Radiobutton(root)
Radiobutton3.configure(justify=LEFT)
Radiobutton3.configure(text='''DX-48''')
Radiobutton3.pack()
Radiobutton4 = Radiobutton(root)
Radiobutton4.configure(justify=LEFT)
Radiobutton4.configure(text='''SX-16VP''')
Radiobutton4.pack()
Radiobutton5 = Radiobutton(root)
Radiobutton5.configure(justify=LEFT)
Radiobutton5.configure(text='''SX-16F''')
Radiobutton5.pack()
Radiobutton6 = Radiobutton(root)
Radiobutton6.configure(justify=LEFT)
Radiobutton6.configure(text='''FX-8''')
Radiobutton6.pack()
Button1 = Button(root)
Button1.configure(pady="0")
Button1.configure(text='''NEXT''')
Button1.pack()
mainloop()
You are clearly creating a grid of widgets, so I recommend using grid. It's well documented and easy to use.
These types of layouts are easiest if you group all of your layout code together rather than interlacing it with your widget creation.
I'm not going to rewrite your whole program, but here's the general idea:
from Tkinter import *
root = Tk()
root.geometry("600x200")
root.grid_columnconfigure((0,1), weight=1)
Label3 = Label(root, text="Device IP Address")
Label4 = Label(root, text="Username")
Label5 = Label(root, text="Password")
Entry3 = Entry(root)
Entry4 = Entry(root)
Entry5 = Entry(root)
Label3.grid(row=3, column=0)
Entry3.grid(row=3, column=1, sticky="ew")
Label4.grid(row=4, column=0)
Entry4.grid(row=4, column=1, sticky="ew")
Label5.grid(row=5, column=0)
Entry5.grid(row=5, column=1, sticky="ew")
root.mainloop()
The above code creates this window:
Related
Below is my code. i would like to know how to start the program with the radio buttons not selected.
from tkinter import *
root = Tk()
root.title("Early Learning Tool")
root.geometry("800x400")
frame = LabelFrame(root, text="Letters", padx=30, pady=10)
frame.pack()
photo_A = PhotoImage(file=r"Pictures\A.png")
photo_B = PhotoImage(file=r"Pictures\B.png")
photo_C = PhotoImage(file=r"Pictures\C.png")
photo_D = PhotoImage(file=r"Pictures\D.png")
photo_E = PhotoImage(file=r"Pictures\E.png")
letter_choice = StringVar()
rb_A = Radiobutton(frame, variable=letter_choice, value="A", image=photo_A)
rb_A.grid(row=0, column=0)
rb_B = Radiobutton(frame, variable=letter_choice, value="B", image=photo_B)
rb_B.grid(row=0, column=1)
rb_C = Radiobutton(frame, variable=letter_choice, value="C", image=photo_C)
rb_C.grid(row=0, column=2)
rb_D = Radiobutton(frame, variable=letter_choice, value="D", image=photo_D)
rb_D.grid(row=0, column=3)
rb_E = Radiobutton(frame, variable=letter_choice, value="E", image=photo_E)
rb_E.grid(row=0, column=4)
root.mainloop()
This is what appears. How do I make it so it doesn't do that?
In letter_choice variable you have to set a default value in StringVar()
like so:
letter_choice = StringVar(value="A")
You need to initialize the StringVar with value other than empty string (default value if not specified) or the values for those radiobuttons, for example ' ':
letter_choice = StringVar(value=' ')
I tried running the programming switching the images to text.
from tkinter import *
root = Tk()
root.title("Early Learning Tool")
root.geometry("800x400")
frame = LabelFrame(root, text="Letters", padx=30, pady=10)
frame.pack()
letter_choice = StringVar()
rb_A = Radiobutton(frame, variable=letter_choice, value="A", text="A")
rb_A.grid(row=0, column=0)
rb_B = Radiobutton(frame, variable=letter_choice, value="B", text="B")
rb_B.grid(row=0, column=1)
rb_C = Radiobutton(frame, variable=letter_choice, value="C", text="C")
rb_C.grid(row=0, column=2)
rb_D = Radiobutton(frame, variable=letter_choice, value="D", text="D")
rb_D.grid(row=0, column=3)
rb_E = Radiobutton(frame, variable=letter_choice, value="E", text="E")
rb_E.grid(row=0, column=4)
root.mainloop()
I didn't encounter any problems. Indeed, the default radiobutton when no buttons are clicked, appears with a small grey dot. But when clicking on a radiobutton the default grey dots disappear.
This is supposed to be a pet company administration GUI app made with tkinter
I am following a tutorial, I don't know why I'm getting this error but the tutor isn't getting it.
I have tried changing the string to int but it's still giving me an error
The program runs when the database is empty, I get this error only when I have data in my database
main.py
from tkinter import *
from tkinter import ttk
from ttkbootstrap import Style
import time
from rex_database import DBConnect
from tkinter import messagebox
dbConnect = DBConnect()
root = Tk()
x = root.winfo_screenwidth()
y = root.winfo_screenheight()
root.geometry("%dx%d" % (x, y))
notebook = ttk.Notebook(root, height=y, width=x)
root.title("Rex Inc System")
style = Style(theme='solar') # bootstrap theme
# tabs
tab1 = Frame(notebook)
tab2 = Frame(notebook)
tab3 = Frame(notebook)
tab4 = Frame(notebook)
tab5 = Frame(notebook)
tab6 = Frame(notebook)
tab7 = Frame(notebook)
# naming tabs
notebook.add(tab1, text="Inventory")
notebook.add(tab2, text="Sales System")
notebook.add(tab3, text="Register Pet")
notebook.add(tab4, text="Adopt Pet")
notebook.add(tab5, text="Studding")
notebook.add(tab6, text="Database")
notebook.add(tab7, text="Calculators")
notebook.grid()
# tab1 content
Label(tab1, text="hbthryhy", fg='green', bg='black', relief="solid", font=("arial", 17, "bold")).grid(row=0, column=0)
# tab2 content
Label(tab2, text="nunujny", fg='blue', bg='black', relief="solid", font=("arial", 17, "bold")).grid(row=0, column=0)
# tab3 content
ttk.Label(tab3, text='Pet Name:').grid(row=0, column=0, pady=10)
petname = ttk.Entry(tab3, width=30, font=('Arial', 10))
petname.grid(row=0, column=1, columnspan=2)
SpanType = StringVar()
SpanType.set("Dog")
ttk.Label(tab3, text='Pet Type:').grid(row=1, column=0)
ttk.Radiobutton(tab3, text="Dog", variable=SpanType, value="Dog").grid(row=1, column=1)
ttk.Radiobutton(tab3, text="Cat", variable=SpanType, value="Cat").grid(row=1, column=2)
SpanType = StringVar()
SpanType.set("Male")
ttk.Label(tab3, text='Gender:').grid(row=2, column=0)
ttk.Radiobutton(tab3, text="Male", variable=SpanType, value="Male").grid(row=1, column=1)
ttk.Radiobutton(tab3, text="Female", variable=SpanType, value="Female").grid(row=1, column=2)
ttk.Label(tab3, text='Pet Description:').grid(row=2, column=0)
pet_desc = ttk.Entry(tab3, width=50)
pet_desc.grid(row=2, column=1)
save_reg = ttk.Button(tab3, text="Save", style='success.TButton')
save_reg.grid(row=3, column=1)
def save_click():
print("Pet Name is {}".format(petname.get()))
print("Pet type is {}".format(SpanType.get()))
print("Pet Description: {}".format(pet_desc.get()))
dbsave = dbConnect.Add(Petname=petname.get(), Type=SpanType.get(), Description=pet_desc.get())
messagebox.showinfo(title="Success", message=dbsave)
petname.delete(0, 'end')
pet_desc.delete(0, 'end')
save_reg.config(command=save_click)
date_reg = time.asctime(time.localtime(time.time()))
Label(tab3, text=date_reg).grid(row=4, column=1)
# tab4 content
Label(tab4, text="grgrgrg", fg='blue', bg='black', relief="solid", font=("arial", 17, "bold")).grid(row=0, column=0)
# tab5 content
Label(tab5, text="thtthhth", fg='blue', bg='black', relief="solid", font=("arial", 17, "bold")).grid(row=0, column=0)
# tab6 content
tv = ttk.Treeview(tab6)
tv.pack()
tv.heading("#0", text="ID")
tv.configure(column=('Petname', 'Type', 'Description'))
tv.heading("Petname", text="Pet Name")
tv.heading("Type", text="Type")
tv.heading("Description", text="Description")
listing = dbConnect.Listpets()
for row in listing:
tv.insert('', 'end', '#{}'.format(row["ID"]), text=row["ID"])
tv.set('#{}'.format(row["ID"]), 'Petname', row["Petname"])
tv.set('#{}'.format(row["ID"]), 'Type', row["Type"])
tv.set('#{}'.format(row["ID"]), 'Description', row["Description"])
# tab7 content
# simple calculator
display = ttk.Entry(tab7, width=77).grid(row=0, column=0, columnspan=2)
root.state("zoomed")
root.mainloop()
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()
Code
list = [ee1.get(), ee2.get(), ee3.get(), ee4.get(), ee5.get(), ee6.get(), ee7.get(), ee8.get(), ee9.get(), ee10.get()]
context = "What does", random.choice(list),"mean?"
labelquestion = Label(window, text = context, font = "Serif 10 bold")
Question
When I run this, it outputs "{What does} {} mean?"
How can I fix this so that it outputs "What does"-random entry from list-"this mean?"
(The things in the list are entries)
Below is an example based on your code and information in the comment:
import tkinter as tk
import random
window = tk.Tk()
ee1 = tk.Entry(window)
ee1.grid(row=0, column=0)
ee2 = tk.Entry(window)
ee2.grid(row=0, column=1)
ee3 = tk.Entry(window)
ee3.grid(row=0, column=2)
ee4 = tk.Entry(window)
ee4.grid(row=0, column=3)
ee5 = tk.Entry(window)
ee5.grid(row=0, column=4)
ee6 = tk.Entry(window)
ee6.grid(row=1, column=0)
ee7 = tk.Entry(window)
ee7.grid(row=1, column=1)
ee8 = tk.Entry(window)
ee8.grid(row=1, column=2)
ee9 = tk.Entry(window)
ee9.grid(row=1, column=3)
ee10 = tk.Entry(window)
ee10.grid(row=1, column=4)
def submit():
words = [ee1.get(), ee2.get(), ee3.get(), ee4.get(), ee5.get(),
ee6.get(), ee7.get(), ee8.get(), ee9.get(), ee10.get()]
context = f"What does '{random.choice(words)}' mean?"
labelquestion.config(text=context)
tk.Button(window, text="Submit", command=submit).grid(row=2, column=0, sticky='w')
labelquestion = tk.Label(window, font="Serif 10 bold")
labelquestion.grid(row=2, column=1, columnspan=4)
window.mainloop()
this code should have given the input data i the console , but my pycharm console is blank without printing the input data also, it doesnt show any kind of error . see the code and help me solve this
i am a beiginner in python and this pycharm ide .help me
from tkinter import *
win = Tk()
win.geometry("400x289")
Label(win, text=" bhandari hotel ", font=" aeiral11 13 bold").grid(row=0, column=2)
name = Label(win, text="name").grid(row=1, column=0)
phone = Label(win, text="phone").grid(row=2, column=0)
gender = Label(win, text="gender").grid(row=3, column=0)
emergency = Label(win, text="emergency").grid(row=4, column=0)
payment = Label(win, text="payment").grid(row=5, column=0)
namevar = StringVar()
phonevar = StringVar()
gendervar = StringVar()
emergencyvar = StringVar()
paymentvar = StringVar()
check = IntVar()
namee = Entry(win, textvariable="namevar")
namee.grid(row=1, column=3)
phonee = Entry(win, textvariable="phonevar")
phonee.grid(row=2, column=3)
gendee = Entry(win, textvariable="gendervar")
gendee.grid(row=3, column=3)
emergencye = Entry(win, textvariable="emergencyvar")
emergencye.grid(row=4, column=3)
paymente = Entry(win, textvariable="paymentvar")
paymente.grid(row=5, column=3)
foodservice = Checkbutton(win, text='do you agree?', variable=check).grid(row=6, column=3)
def amazing():
print("hello"+ str(namevar.get()))
Button(text="submit", bg='gold', command=amazing).grid()
win.mainloop()
please help me solve this .
namee = Entry(win, textvariable=namevar)
namee.grid(row=1, column=3)
phonee = Entry(win, textvariable=phonevar)
phonee.grid(row=2, column=3)
gendee = Entry(win, textvariable=gendervar)
gendee.grid(row=3, column=3)
emergencye = Entry(win, textvariable=emergencyvar)
emergencye.grid(row=4, column=3)
paymente = Entry(win, textvariable=paymentvar)
paymente.grid(row=5, column=3)
don't assign the variable in strings. here's the change.
It's just a suggestion
use :
a=Entry(..)
a.grid(..)
instead of
a=Entry(...).grid(...)
whole modified code:
from tkinter import *
win = Tk()
win.geometry("400x289")
Label(win, text=" bhandari hotel ", font=" aeiral11 13 bold").grid(row=0, column=2)
name = Label(win, text="name").grid(row=1, column=0)
phone = Label(win, text="phone").grid(row=2, column=0)
gender = Label(win, text="gender").grid(row=3, column=0)
emergency = Label(win, text="emergency").grid(row=4, column=0)
payment = Label(win, text="payment").grid(row=5, column=0)
namevar = StringVar()
phonevar = StringVar()
gendervar = StringVar()
emergencyvar = StringVar()
paymentvar = StringVar()
check = IntVar()
namee = Entry(win, textvariable=namevar)
namee.grid(row=1, column=3)
phonee = Entry(win, textvariable=phonevar)
phonee.grid(row=2, column=3)
gendee = Entry(win, textvariable=gendervar)
gendee.grid(row=3, column=3)
emergencye = Entry(win, textvariable=emergencyvar)
emergencye.grid(row=4, column=3)
paymente = Entry(win, textvariable=paymentvar)
paymente.grid(row=5, column=3)
foodservice = Checkbutton(win, text='do you agree?', variable=check).grid(row=6, column=3)
def amazing():
print("hello "+ str(namevar.get()))
Button(text="submit", bg='gold', command=amazing).grid()
win.mainloop()