How to set python window center position - python

i want set python window center position when the form is loaded how set.what i tried so far i attached below
from tkinter import *
from tkinter import messagebox
from subprocess import call
root = Tk()
root.title("Main")
root.geometry("500x500")
global e1
global e2
def Ok():
call(["python", "Main.py"])
Label(root, text="Welcome").place(x=10, y=10)
Button(root, text="Add Student", command=Ok ,height = 3, width = 13).place(x=10, y=100)
root.mainloop()

Does this center your window?
from tkinter import *
from tkinter import messagebox
from subprocess import call
root = Tk()
root.title("Main")
window_width,window_height = 500,500
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
position_top = int(screen_height/2 - window_height/2)
position_right = int(screen_width / 2 - window_width/2)
root.geometry(f'{window_width}x{window_height}+{position_right}+{position_top}')
def Ok():
pass
Label(root, text="Welcome").place(x=10, y=10)
Button(root, text="Add Student", command=Ok ,height = 3, width = 13).place(x=10, y=100)
root.mainloop()

Related

python importing file but image not recognized

I have created one python file with a drowdown menu. When i choose the option one, it imports another python file, with a checkbutton and a picture in a canvas. Both files and the picture are located in the same folder. The code import the file imports the canvas and the checkbutton, but I get the error saying image "pyimage1" doesn't exist. If I run that second file alone, it does show the checkbutton and the image without errors. When Import a python file the images are not recognized anymore or am I doing something wrong? is any workaround there?
main program:
from tkinter import *
root = Tk()
root.geometry('1560x750')
canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)
def option_number(x):
if x == "one":
import part2
variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)
root.mainloop()
file to be imported:
from tkinter import *
root = Tk()
root.geometry('1560x750')
canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)
button = Checkbutton(canvas).place(x=170, y=230)
AND_gate=PhotoImage(file='AND.png') #set up variables for and_gate
labelimage_and = Label(canvas, image=AND_gate).place(x=200,y=200)
root.mainloop()
Updated code to import function:
from tkinter import *
root = Tk()
root.geometry('1560x750')
canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)
def option_number(x):
if x == "one":
from part1 import import_def
variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)
root.mainloop()
file where is the function to be imported:
from tkinter import *
root = Tk()
def import_def():
root = Toplevel()
root.geometry('1560x750')
canvas2 = Canvas(root)
canvas2.config(width=1000, height=1560, bg='red')
canvas2.grid(row=1, column=3, rowspan=1550, ipadx=1300, ipady=750, sticky=NW)
button = Checkbutton(canvas2).place(x=170, y=230)
AND_gate=PhotoImage(file='AND.png') #set up variables for and_gate
labelimage_and = Label(canvas2, image=AND_gate).place(x=200,y=200)
root.mainloop()
This is the way that I know on how to import files and functions within tkinter, not sure if this is the right way but take a look at the changes I made to both the files
main.py:
from tkinter import *
from function import import_def
root = Tk()
root.geometry('1560x750')
canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)
def option_number(x):
if x == "one":
import_def()
variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)
root.mainloop()
and function.py:
from tkinter import *
def import_def():
root = Toplevel()
root.geometry('1560x750')
canvas2 = Canvas(root)
canvas2.config(width=1000, height=1560, bg='red')
canvas2.grid(row=1, column=3, rowspan=1550, ipadx=1300, ipady=750, sticky=NW)
button = Checkbutton(canvas2).place(x=170, y=230)
AND_gate=PhotoImage(file='sad songs.jpg') #set up variables for and_gate
labelimage_and = Label(canvas2, image=AND_gate).place(x=200,y=200)
root.mainloop()
Hope it was of some help, do let me know if any doubts or errors.
Cheers

can't invoke "wm" command: application has been destroyed

import tkinter as tk
from tkinter import *
import pyautogui
root = tk.Tk()
#label = tk.Label(root, text='you coomand hacking', font= ('Times New Roman','80'), fg='black', bg='white')
#label.pack()
root.title("Wallpaper")
wall = PhotoImage(file = "20170227180026_3.gif")
wall_label = Label(image = wall)
root.geometry(f"{wall.width()}x{wall.height()}")
root.overrideredirect(True)
currentMouseX,currentMouseY = pyautogui.position()
print(f'{currentMouseX} {currentMouseY}')
root.geometry("+0+0")
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
wall_label.place(x = -2,y = -2)
root.after(1000 , root.destroy)
#root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
root.mainloop()
root.after(1000 , root.destroy)
root.geometry("+{}+{}".format(currentMouseX,currentMouseY))
Please understand even if the source code is dirty.
What I want is for the image to follow the mouse
I tried to follow the mouse by repeating the image disappearing and appearing, but it is not easy.
Here. I made the image follow the mouse, without pyautogui.
Code:
import tkinter as tk
from tkinter import *
#import pyautogui
root = tk.Tk()
#label = tk.Label(root, text='you coomand hacking', font= ('Times New Roman','80'), fg='black', bg='white')
#label.pack()
root.title("Wallpaper")
wall = PhotoImage(file = "20170227180026_3.gif")
c = Canvas(root, height = 1200, width = 1200)
c.pack()
pic = c.create_image(600, 600, image = wall)
def movepic(event):
x,y = event.x, event.y
c.coords(pic, x, y)
c.bind_all("<Motion>", movepic)
root.mainloop()

How to insert image into different columns in tkinter Treeview widget?

I just want, that image was in specified (in column named 'Country') column instead of first. Thanks!
from tkinter import ttk
from tkinter import *
root = Tk()
s = ttk.Treeview(columns=('#1', '#2'))
s.heading('#0', text='Ip')
s.heading('#1', text='Port')
s.heading('#2', text='Country')
s.pack()
v = PhotoImage(file='uk.png')
s.insert('', 2, values=('127.0.0.1', '8888'), image=v)
root.mainloop()
I've made it simple :) just starting with country
from tkinter import ttk
from tkinter import *
root = Tk()
s = ttk.Treeview(columns=('#1', '#2'))
s.heading('#0', text='Country')
s.heading('#1', text='Ip')
s.heading('#2', text='Port')
v = PhotoImage(file='uk.png')
s.insert('', 2, values = ('127.0.0.1', '8888'), image=v)
s.pack()
root.mainloop()
If you don't like the image in the first place, I can imagine this, without treeview, though
from tkinter import ttk
from tkinter import *
root = Tk()
v = PhotoImage(file='uk.png')
# header
b = Label(root, text="Ip")
b.grid(row=0, column=0)
b = Label(root, text="port")
b.grid(row=0, column=1)
b = Label(root, text="Country")
b.grid(row=0, column=2)
lista = [["800","127.1.1.",v]]
height = 2
width = 3
for i in range(height-1): #Rows
b1 = Label(root, text=lista[i][0])
b1.grid(row=i+1, column=0)
b2 = Label(root, text=lista[i][1])
b2.grid(row=i+1, column=1)
b3 = Label(root, image=lista[i][2])
b3.grid(row=i+1, column=2)
mainloop()

Use tkinter buttons to control a Python turtle in another window

I'm trying to use tkinter's buttons to control a turtle in another graphics window, but the buttons don't function
I was trying to use turtle graphics and Tkinter to make a simple program that controls a turtle with buttons. However, it seems that only one button is actually running a function, and even that is producing an error. Here is the code I wrote:
from tkinter import *
import turtle
global x
global y
x = 1
y = 1
i = 0
root = Tk()
wn = turtle.Screen()
bob = turtle.Turtle()
bob.up()
def goUp(event):
y=y+5
bob.goto(x,y)
def goDown(event):
y=y-5
bob.goto(x,y)
def goRight(event):
x=x+5
bob.goto(x,y)
def goLeft(event):
x=x-5
bob.goto(x,y)
topFrame = Frame(root)
topFrame.pack()
middleFrame = Frame(root)
middleFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack()
button1 = Button(topFrame, text = "Up", fg="red")
button2 = Button(middleFrame, text = "Left", fg="red")
button3 = Button(middleFrame, text = "Right", fg="red")
button4 = Button(bottomFrame, text = "Down", fg="red")
button1.bind("<Button-1>", goUp)
button2.bind("<Button-2>", goLeft)
button3.bind("<Button-3>", goRight)
button4.bind("<Button-4>", goDown)
button1.pack()
button2.pack(side=LEFT)
button3.pack(side=RIGHT)
button4.pack()
root.mainloop()
The TKinter Button class accepts a command option where you can pass in the function that gets called when a button is pressed. You also need to access the global x and y variables inside your methods.
Try this code:
from tkinter import *
import turtle
global x
global y
x = 1
y = 1
i = 0
root = Tk()
wn = turtle.Screen()
bob = turtle.Turtle()
bob.up()
def goUp():
global x, y
y=y+5
bob.goto(x,y)
def goDown():
global x, y
y=y-5
bob.goto(x,y)
def goRight():
global x, y
x=x+5
bob.goto(x,y)
def goLeft():
global x, y
x=x-5
bob.goto(x,y)
topFrame = Frame(root)
topFrame.pack()
middleFrame = Frame(root)
middleFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack()
button1 = Button(topFrame, text = "Up", fg="red", command=goUp)
button2 = Button(middleFrame, text = "Left", fg="red", command=goLeft)
button3 = Button(middleFrame, text = "Right", fg="red", command=goRight)
button4 = Button(bottomFrame, text = "Down", fg="red", command=goDown)
button1.pack()
button2.pack(side=LEFT)
button3.pack(side=RIGHT)
button4.pack()
root.mainloop()
Turtle's Turtle and Screen classes are generally used when turtle.py is used standalone. When turtle, which is implemented atop tkinter, is used embedded inside tkinter, we generally use the RawTurtle and TurtleScreen classes instead. You'll find more about this in the turtle documentation
You also should review one of the many online Python global keyword tutorials as you're not using it correctly. Fortunately, we don't need it to implement this particular program.
Here's a rework of your code, as a single window implementation, that illustrates the above:
import tkinter as tk
from turtle import RawTurtle, TurtleScreen
def goUp():
bob.sety(bob.ycor() + 5)
def goDown():
bob.sety(bob.ycor() - 5)
def goRight():
bob.setx(bob.xcor() + 5)
def goLeft():
bob.setx(bob.xcor() - 5)
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
wn = TurtleScreen(canvas)
bob = RawTurtle(wn, shape="turtle")
bob.penup()
topFrame = tk.Frame(root)
topFrame.pack()
middleFrame = tk.Frame(root)
middleFrame.pack()
bottomFrame = tk.Frame(root)
bottomFrame.pack()
tk.Button(topFrame, text="Up", fg="red", command=goUp).pack()
tk.Button(middleFrame, text="Left", fg="red", command=goLeft).pack(side=tk.LEFT)
tk.Button(middleFrame, text="Right", fg="red", command=goRight).pack(side=tk.RIGHT)
tk.Button(bottomFrame, text="Down", fg="red", command=goDown).pack()
wn.mainloop()

Python canvas colour change by button

I am a python self learner. I was stuck on some practice.
My idea was to create a pop out GUI with buttons that can change the canvas colour.
from Tkinter import *
import ttk
import tkMessageBox
root = Tk()
root.title("Colour!")
canvasColor = "yellow"
def buttonRed() :
canvas = Canvas(root, bg = "red", height=100, width=100)
canvas.grid(row=0,column=2)
button = ttk.Button(root, text="Red", command = buttonRed)
button.grid(row=2,column=1)
button2 = ttk.Button(root, text ="Green", command = buttonGreen)
button2.grid(row=2,column=2)
button3 = ttk.Button(root, text="Blue", command = buttonBlue)
button3.grid(row=2,column=3)
canvas = Canvas(root, bg = canvasColor, height=200, width=200)
canvas.grid(row=0,column=2)
root.configure(background='white')
root.mainloop()
i haven't put in the green and blue button command yet, but instead of creating a new canvas when the colour button clicked, i just wanted to have the default canvas colour change.
Any help will be much appreciated!
Thanks in advance.
I think this is what you need -
from Tkinter import *
import ttk
import tkMessageBox
root = Tk()
root.title("Colour!")
canvasColor = "yellow"
def buttonRed() :
canvas.config(background="red")
def buttonGreen() :
canvas.config(background="green")
def buttonBlue() :
canvas.config(background="blue")
button = ttk.Button(root, text="Red", command = buttonRed)
button.grid(row=2,column=1)
button2 = ttk.Button(root, text ="Green", command = buttonGreen)
button2.grid(row=2,column=2)
button3 = ttk.Button(root, text="Blue", command = buttonBlue)
button3.grid(row=2,column=3)
canvas = Canvas(root, bg = canvasColor, height=200, width=200)
canvas.grid(row=0,column=2)
#canvas.config(background="black")
root.configure(background='white')
root.mainloop()

Categories

Resources