Why window size out of screen tkinter? - python-3.x

I created a billing system in my Linux system.in Linux my code works fine and fit to screen
I use place() in code
When I run the same code in windows 10 window size enlarge and shows half of gui
Help me how to fix it
No errors in code but problem in only window size
How to adjust
https://github.com/vijaySai-22/python/tree/main/billing
import tkinter as tk
from tkinter import *
class App():
def __init__(self,root):
##########--------------Company name-------------##########
Label(root,text="Green Vegetables Market",font=('Arial',30,'bold','underline'),width=(84),bg="black",fg="white").place(x=0,y=0)
Label(root,text="Customer Details",font=('Arial',30),width=(84),bg="grey",fg="black").place(x=0,y=60)
Label(root,text="Enter Custmor Name",font=('Arial',18)).place(x=5,y=120)
self.c_name=tk.StringVar()
self.cname_entry=Entry(root,font=('Arial',18),width=(20),textvariable=self.c_name).place(x=245,y=120)
Label(root,text="Enter contact number",font=('Arial',18)).place(x=525,y=120)
self.c_phno=tk.StringVar()
self.phno_entry=Entry(root,font=('Arial',18),width=(20),textvariable=self.c_phno).place(x=769,y=120)
Label(root,text="Enter Address",font=('Arial',18)).place(x=1045,y=120)
self.c_add=tk.StringVar()
self.add_entry=Entry(root,font=('Arial',18),width=(46),textvariable=self.c_add).place(x=1210,y=120)
# Label(root,text="Age",font=('Arial',18)).place(x=1695,y=120)
# c_age=tk.StringVar()
# self.age_entry=Entry(root,font=('Arial',18),width=(4)).place(x=1750,y=120)
##########---------------Particulars-------------###########
Label(root,text="Particulars",font=('Arial',30),fg='black',bg='grey',width=(50)).place(x=5,y=180)
self.veg=['Potato','Tomato','Carrot','Brinjal','Onions','Cabage','Ginger','Chilli','Garlic','Beetroot']
self.veg_price=[20,22,30,25,30,30,70,32,60,53]
self.ck=[None]*len(self.veg)
self.qty=[None]*len(self.veg)
self.dis=[None]*len(self.veg)
self.each_price=[None]*len(self.veg)
self.y_value=300
Label(root,text=" ItemNo Vegetables Qty Discount Total ",font=('Arial',23)).place(x=10,y=240)
for i in range(len(self.veg)):
self.item_no='Item'+str(i+1)
Label(root,text=self.item_no,font=('Arial',23)).place(x=50,y=self.y_value)
self.ck[i]=tk.IntVar()
name = Checkbutton(root, text = self.veg[i],variable = self.ck[i],
onvalue = 1,offvalue = 0,
font=('Arial',23),width = 18,anchor="w"
).place(x=180,y=self.y_value)
self.qty[i]=tk.DoubleVar()
self.qty[i].set(0)
Entry(root,font=('Arial',23),textvariable=self.qty[i],width=3).place(x=600,y=self.y_value)
Label(root,text=".kg",font=('Arial',23)).place(x=660,y=self.y_value)
self.dis[i]=tk.DoubleVar()
self.dis[i].set(0)
Entry(root,font=('Arial',23),textvariable=self.dis[i],width=3).place(x=760,y=self.y_value)
Label(root,text="% ",font=('Arial',23)).place(x=820,y=self.y_value)
self.each_price[i]=Label(root,text="0 Rs",font=('Arial',23))
self.each_price[i].place(x=950,y=self.y_value)
self.y_value=self.y_value+60
self.ext=Button(root,text="Exit",font=('Arial',23),bg="red",fg="black",command=self.close).place(x=50,y=900)
self.clr=Button(root,text="Clear",font=('Arial',23),bg="yellow",fg="black",command=self.clear).place(x=600,y=900)
self.cal=Button(root,text="Total=",font=('Arial',23),bg="green",fg="black",command=self.calculate).place(x=760,y=900)
self.total_price=Label(root,text="0 RS",font=('Arial',27),width=9)
self.total_price.place(x=900,y=900)
##########-----------line-------------##########
Label(root,font=(30),fg='black',bg='grey',width=(3),height=(40)).place(x=1100,y=180)
##########-----------Bill-------------##########
Label(root,text="Bill",font=('Arial',30),fg='black',bg='grey',width=(32)).place(x=1130,y=180)
self.bill_text=Text(root)
self.bill_text.insert("insert", "\n\nGreen Vegetables Market\n")
self.bill_text.insert("end","One and only market where you can find Organic Vegetables\nSrikakulam, Andhrapradesh, 532484\nContact 9900887766\nGST no:X1234567890XYZ")
self.bill_text.tag_add("head", "3.0", "3.23")
self.bill_text.tag_add("rest", "4.0", "7.22")
self.bill_text.tag_config("head", font=('Arial',30,'bold','underline'),justify='center')
self.bill_text.tag_config("rest", font=('Arial',15),justify='center')
self.bill_text.config(state='disabled',width=75,height=38)
self.bill_text.place(x=1180,y=240)
self.complete_and_save=Button(root,text="Complete and Save",font=('Arial',23),command=self.save_bill).place(x=1500,y=900)
def calculate(self):
if self.c_name.get()!='' and self.c_phno.get()!='' and self.c_add.get()!='':
tot=0.0
line=13.0
self.bill_text.config(state='normal')
self.bill_text.delete("8.0","end")
for i in range(len(self.veg)):
each_tot=0.0
if self.ck[i].get()==1 and self.qty[i].get()>0:
self.bill_text.insert("end", "\nCustomer Details\n\tCustmor Name: "+self.c_name.get()+
"\n\tContact number: "+self.c_phno.get()+"\n\tAddress: "+self.c_add.get())
self.bill_text.tag_add("details_head", "8.0","8.16")
self.bill_text.tag_config("details_head", font=('Arial',20,'underline'),justify='center')
self.bill_text.tag_add("details_body", "9.0","11.50")
self.bill_text.tag_config("details_body", font=('Arial',15),justify='left')
self.bill_text.insert("end", "\n\n"+"\t"+"item"+"\t\t"+"qty"+"\t\t"+"dis"+"\t\t"+"Total\n")
each_tot=each_tot+self.veg_price[i]*self.qty[i].get()
each_tot=each_tot-((each_tot/100)*self.dis[i].get())
self.each_price[i].config(text=str(each_tot)+" Rs")
tot=tot+each_tot
self.total_price.config(text=str(tot)+" Rs")
self.bill_text.config(state='normal')
self.bill_text.insert("end","\n"+"\t"+self.veg[i]+"\t\t"+str(self.qty[i].get())+"\t\t"+str(self.dis[i].get())+"\t\t"+str(each_tot))
self.bill_text.tag_add("items",str(line),"end")
self.bill_text.tag_config("items",font=('Arial',15),justify='left')
self.bill_text.config(state='disabled')
line=line+1
else:
self.each_price[i].config(text="0 Rs")
self.qty[i].set(0)
self.dis[i].set(0)
self.bill_text.config(state='normal')
self.bill_text.insert("end", "\n\n\t\t\t\t\t GST(18%) :"+"{:.2f}".format(((tot)/100)*18))
self.bill_text.insert("end", "\n\n\t\t\t\t\t Total amt :"+"{:.2f}".format((tot+((tot)/100)*18)))
self.bill_text.config(state='disabled')
def close(self):
exit()
def clear(self):
for i in range(len(self.veg)):
self.ck[i].set(0)
self.each_price[i].config(text="0 Rs")
self.qty[i].set(0)
self.dis[i].set(0)
self.bill_text.config(state='normal')
self.bill_text.delete("8.0","end")
self.bill_text.config(state='disabled')
def save_bill(self):
import datetime
from tkinter import messagebox
current_time = datetime.datetime.now()
self.file_name=str(self.c_name)+str(current_time)
self.file=open(self.file_name+".txt","w")
self.file.write(self.bill_text.get("1.0","end"))
self.file.close()
self.clear()
messagebox.showinfo("showinfo", "Saved Successfully")
if __name__ == "__main__":
win=tk.Tk()
#title
win.title("billing system")
#setting tkinter window size
width= win.winfo_screenwidth()
height= win.winfo_screenheight()
win.geometry("%dx%d" % (width, height))
#bg color
win.config(bg="black")
app=App(win)
win.mainloop()

You hardcoded (x, y) positions for all your widgets, so it only fits on that resolution. Instead, you should have fullscreened, got window height & width info, then positioned widgets as divisions of those dimensions.
root .attributes( '-zoomed', True )
root .update_idletasks()
Width, Height = root .winfo_width(), root .winfo_height()
self .clr = Button( root, text = "Clear", font = ( 'Arial', 23 ), bg = "yellow",
fg = "black", command = self .clear ) .place( x = 600, y = 900 )
should read something more along the lines of:
self .clr = Button( root, text = "Clear", font = ( 'Arial', 23 ), bg = "yellow",
fg = "black", command = self .clear ) .place( x = Width *0.5, y = Height *0.75 )
Or use .grid() instead of .place() - it does a lot of that for you -- https://www.geeksforgeeks.org/python-grid-method-in-tkinter/

Related

Im having troubles with python(tkinter) gui with the layout

# Import module
from tkinter import *
import pyautogui, time
# Create object
root = Tk()
# Adjust size
root.geometry("350x350")
root.title("SycoBak's SpamBot")
# Add image file
bg = PhotoImage(file = "Slogo.png")
# Create Canvas
canvas1 = Canvas( root, width = 350,
height = 350)
canvas1.pack(fill = "both", expand = True)
# Display image
canvas1.create_image( 0, 0, image = bg,
anchor = "nw")
canvas1.create_text(250,250, text="SycoSpamBot")
L1 = Label(canvas1, text="Word")
L1.pack( side = LEFT)
E1 = Entry(canvas1)
E1.pack(side = RIGHT)
canvas1.create_window(100, 100, window=L1)
L2 = Label(canvas1, text="Amount")
L2.pack( side = LEFT)
E2 = Entry(canvas1)
E2.pack(side = RIGHT)
canvas1.create_window(100, 150, window=L2)
def Spammer ():
time.sleep(5)
x = 0
amount = int(E2.get())
word = E1.get()
while (amount > x):
pyautogui.typewrite(word)
pyautogui.press("enter")
x=x+1
button1 = Button(root, text = "Start", command=Spammer, bg="green")
button1_canvas = canvas1.create_window( 100, 10,
anchor = "nw",
window = button1)
root.mainloop()
Im wondering if I could make it so that the start button is at the bottom, and the word and amount labels are on the left side of a text input box.
This is how my GUI layout currently looks like. (Very Messy)
[1]: https://i.stack.imgur.com/UYvtU.png

Drawing shapes in place of using image

So I have a class that displays some text with a background and an image of a shape that goes along with it. I want to create/draw the shapes inside this class instead of using an actual image file. I found some code that draws a circle/diamond but I have been having trouble implementing it inside my class. What would be some ways to do this?
This is my class:
from tkinter import Tk, Button, Label, BOTTOM, CENTER
from PIL import ImageTk, Image
# set properties of window
window_width = 1920
window_height = 238
# set properties of image
# NOTE: pixel dims of image file mapped 1 to 1 on window
image_width = 238
image_height = 238
# set properties of label
label_width = window_width - image_width
label_height = 238
background_color = 'navy'
foreground_color = 'yellow'
# set properties of font
font_family = 'Helvetica'
font_height = 110
font_weight = 'bold'
# variables for text switching
testIndex = 0
text = [
'Test Test Test',
'TEST TEST !!!',
'12345678901234567890'
]
class GUIClass:
def __init__(self, root):
# bringing text variables into class scope
self.root = root
self.testIndex = testIndex
self.text = text
# creating image for sign
self.image = ImageTk.PhotoImage(Image.open("testImage.png"))
self.imageLabel = Label(image=self.image, width=image_width, height=image_height, bg=background_color)
self.imageLabel.place(x=0, y=0, width=image_width, height=image_height)
# creating label, setting font, position, and binding left-click event listener
self.label = Label(root, text=text[testIndex], bg=background_color, fg=foreground_color, borderwidth=10, relief="solid")
self.label['font'] = (font_family, font_height, font_weight)
self.label.bind("<Button-1>", self.closeScreen)
self.label.place(x=image_width, y=0, width=label_width, height=window_height)
# creating close button to close window (necessary if removing border of window)
#self.close_button = Button(root, text='close', command= root.quit)
#self.close_button.pack(side=BOTTOM)
#set timer to cycle through strings
self.root.after(1500, self.timerTest)
# threaded event handler for when timer elapses
def timerTest(self):
self.testIndex = self.testIndex + 1
self.testIndex %= len(self.text)
self.label['text'] = self.text[self.testIndex]
self.root.after(1500, self.timerTest)
# closes window when label is left-clicked
def closeScreen(self, event):
self.root.quit()
# creates the window
root = Tk()
# sets dimensions of window (ex: "200x200")
root.geometry(str(window_width) + "x" + str(window_height))
# removes boundary, disables resizing, and removes buttons of window
root.overrideredirect(True)
# attaches the window to the GUI class
rootGUI = GUIClass(root)
# continously loops, draws display, and waits for user input
root.mainloop()
This is the code I found for the circle/diamond shape:
from tkinter import Tk, Canvas, Frame, BOTH
class Example(Frame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.master.title("Shapes")
self.pack(fill=BOTH, expand=1)
canvas = Canvas(self)
canvas.create_oval(5, 5, 233, 233, outline="#fb0", fill="#fb0", width=0)
points = [119, 5, 233, 119, 119, 233, 5, 119]
canvas.create_polygon(points, outline='#fb0', fill='#fb0', width=0)
canvas.pack(fill=BOTH, expand=1)
def main():
root = Tk()
ex = Example()
root.geometry("330x220+300+300")
root.mainloop()
if __name__ == '__main__':
main()

Trouble filling a Python tkinter/turtle window with squares

I'm having trouble with getting the turtle.begin_fill() / turtle.end_fill() commands to work in a tkinter window. I want to have the program draw a bunch of coloured cells onto the screen in a 64x32 grid. This the whole code to set up the window and draw however it simply doesn't do anything after setting up a window:
import turtle as tu
import tkinter as tk
axis = []
num = -512
for i in range(0,64):
axis.append(num)
num = num + 16
def drawworld(*args):
for i in range(0,64):
for j in range(0,32):
tu.goto(axis[i],axis[j]+256)
tu.pd()
tu.color=("#ffff00")
tu.begin_fill()
for k in range(0,4):
tu.fd(16)
tu.lt(90)
tu.end_fill()
tu.pu()
root = tk.Tk()
root.title("game")
root.resizable(False, False)
canvas = tk.Canvas(master = root, width = 1024, height = 512)
canvas.grid(row=0, column=0, columnspan=10)
tu = tu.RawTurtle(canvas)
tu.speed(50)
tu.pu()
tu.ht()
drawworld()
root.mainloop()
however, if I were to comment out the fill lines, the code works perfectly (without colouring the boxes) which means there must be something wrong with:
tu.color=("#ffff00")
tu.begin_fill()
for k in range(0,4):
tu.fd(16)
tu.lt(90)
tu.end_fill()
tu.pu()
I've looked up the documentation and this should be perfect syntax. What am I doing wrong?
I believe your problem is this line:
tu.color=("#ffff00")
which probably should be:
tu.color("#ffff00")
to set the color, you don't assign to the turtle's color property but rather invoke the turtle's .color() method. Your code with this change and some other cleanup:
import tkinter as tk
from turtle import RawTurtle
axis = []
num = -512
for _ in range(64):
axis.append(num)
num += 16
def drawworld():
for i in range(64):
for j in range(32):
tu.goto(axis[i], axis[j] + 256)
tu.pendown()
tu.color("#ffff00")
tu.begin_fill()
for _ in range(4):
tu.forward(16)
tu.left(90)
tu.end_fill()
tu.penup()
root = tk.Tk()
root.title("game")
root.resizable(False, False)
canvas = tk.Canvas(master=root, width=1024, height=512)
canvas.grid(row=0, column=0, columnspan=10)
tu = RawTurtle(canvas)
tu.speed('fastest')
tu.hideturtle()
tu.penup()
drawworld()
root.mainloop()
However, this is a painfully slow way to go about this. There are faster ways, here's one that uses stamping to speed up the process:
import tkinter as tk
from turtle import RawTurtle
from random import random
CUBE_SIZE = 16
CURSOR_SIZE = 20
WIDTH, HEIGHT = 1024, 512
axis = []
num = -HEIGHT
for _ in range(WIDTH // CUBE_SIZE):
axis.append(num)
num += CUBE_SIZE
def drawworld():
for i in range(WIDTH // CUBE_SIZE):
tu.goto(axis[i] + CUBE_SIZE // 2, axis[0] + HEIGHT // 2 + CUBE_SIZE // 2)
for j in range(HEIGHT // CUBE_SIZE):
tu.color(random(), random(), random())
tu.stamp()
tu.forward(CUBE_SIZE)
root = tk.Tk()
root.title("game")
root.resizable(False, False)
canvas = tk.Canvas(master=root, width=WIDTH, height=HEIGHT)
canvas.grid(row=0, column=0, columnspan=10)
tu = RawTurtle(canvas)
tu.shape('square')
tu.shapesize(CUBE_SIZE / CURSOR_SIZE)
tu.speed('fastest')
tu.hideturtle()
tu.setheading(90)
tu.penup()
drawworld()
root.mainloop()

Tkinter Label: Retain Height when I change font to smaller size

I have an Label for a quite advanced calculator app.
I'd like to be able to change the font size when the text entered gets too long, to be able to enter more, but retain the height of the widget just for good looks.
Here's piece of code I tried:
self.biglabelfont = font.Font(family = "Arial", size = 24, weight = "bold")
self.entrylabel = Label(... font = self.biglabelfont ....)
self.entrylabel.pack(side = LEFT, fill = "x", expand = True)
self.entrylabel.propagate(0)
self.biglabelfont = font.Font(family = "Arial", size = 11, weight = "bold")
self.entrylabel.config(font = self.biglabelfont)
But it keeps shrinking the height.
Your choice to use pack_propagate is good, but you're doing it wrong. You must call it on the parent of the label widget, not on the label widget.
Here's a working example. Notice that the height of the sunken area doesn't change as the font grows and shrinks.
import Tkinter as tk
import tkFont
BG = "white"
HEIGHT = 75
def main():
global the_font, root
root = tk.Tk()
the_font = tkFont.Font(family="helvetica", size=18)
toolbar = make_toolbar(root)
subframe = make_subframe(root)
toolbar.pack(side="top", fill="x")
subframe.pack(side="top", fill="x")
root.geometry("400x400")
root.mainloop()
def make_subframe(parent):
frame = tk.Frame(parent, height=HEIGHT, borderwidth=1, relief="sunken", background=BG)
frame.pack_propagate(False)
label = tk.Label(frame, font=the_font, text="0123455.67890", background=BG)
label.pack(side="top", fill="both", expand=True)
return frame
def font_plus():
size = the_font.cget("size")
size += 2
the_font.configure(size=size)
def font_minus():
size = the_font.cget("size")
size -= 2
the_font.configure(size=size)
def make_toolbar(parent):
toolbar = tk.Frame(root)
grow = tk.Button(toolbar, text="Bigger", command=font_plus)
shrink = tk.Button(toolbar, text="Smaller", command=font_minus)
grow.pack(side="left")
shrink.pack(side="left")
return toolbar
main()

Generate 10 balls in tkinter canvas

Ok guys.
I am trying to generate 10 balls of random color in Tkinter canvas when I click the generate button.
Program works, and random color choice works for the ball, but I only get one ball generated at a time.
Every time I click the button it randomly moves the ball around, but all I want is 10 balls in 10 random positions at a time. I am using Python 3.4 on a Linux box.
This is a code I've got:
from tkinter import *
import random # to generate random balls
colors = ["red", "blue", "purple", "green", "violet", "black"]
class RandomBalls:
"""
Boilerplate code for window in Tkinter
window = Tk()
window.title("Random title")
window.mainloop()
"""
def __init__(self):
"""
Initialize the window and add two frames, one with button, and another one with
canvas
:return:
"""
window = Tk()
window.title("Random balls")
# A canvas frame
frame1 = Frame(window)
frame1.pack()
self.canvas = Canvas(frame1, width = 200, height = 300, bg = "white")
self.canvas.pack()
# A button frame
frame2 = Frame(window)
frame2.pack()
displayBtn = Button(frame2, text = "Display", command = self.display)
displayBtn.pack()
window.mainloop()
def display(self):
for i in range(0, 10):
self.canvas.delete("circle") # delete references to the old circle
self.x1 = random.randrange(150)
self.y1 = random.randrange(200)
self.x2 = self.x1 + 5
self.y2 = self.y1 + 5
self.coords = self.x1, self.y1, self.x2, self.y2
self.canvas.create_oval(self.coords, fill = random.choice(colors), tags = "circle")
self.canvas.update()
RandomBalls()
Every time through your loop you are deleting everything you created before, including what you created the previous iteration. Move the delete statement outside of the loop:
def display(self):
self.canvas.delete("circle")
for i in range(0, 10):
...

Resources