I have this simple Tkinter Custom Window. I am a beginner and only learnt tkinter a few months ago. I have no experience in real software development. So, I would like to know if the way it was coded is acceptable? I know that when i say acceptable , it could mean a lot of things. I just want to know what are the things i should improve in my coding style & the way i think?
import Tkinter as tk
''' Creating Tkinter Tk instance '''
class Application(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
self.bind("<ButtonPress-1>", self.StartMove)
self.bind("<ButtonRelease-1>", self.StopMove)
self.bind("<B1-Motion>", self.OnMotion)
self.Init()
self.Layout()
self.AddButtons()
''' Setting Main Tk window size & styles '''
def Init(self):
self.geometry("1280x700+0+0")
self.overrideredirect(True)
self['background'] = '#201F29'
self['highlightthickness'] = 2
self['relief'] = 'groove'
'''Layout of the Tk window'''
def Layout(self):
self.exitmenu = tk.Frame(self)
self.exitmenu.place(x = 1217, y = 0)
self.container = tk.Frame(self,width = 1268,height = 648 , relief = 'flat',bd = 0)
self.container.place(x = 5,y = 40)
''' Adding Exit button and Minimize button to the Tk window'''
def AddButtons(self):
self.minibutton = tk.Button(self.exitmenu,text = '0',font=('webdings',8,'bold'),relief = 'flat' , command = self.minimize )
self.minibutton.pack(side = 'left')
self.exitbutton = tk.Button(self.exitmenu,text = 'r',font=('webdings',8),relief = 'flat' ,bg = '#DB6B5A', command = self.destroy )
self.exitbutton.pack(side = 'left')
def minimize(self):
self.overrideredirect(False)
self.wm_state('iconic')
self.overrideredirect(True)
'''Methods for moving window frame'''
def StartMove(self, event):
self.x = event.x
self.y = event.y
def StopMove(self, event):
self.x = None
self.y = None
def OnMotion(self, event):
x1 = self.x
y1 = self.y
x2 = event.x
y2 = event.y
deltax = x2 - x1
deltay = y2 - y1
a = self.winfo_x() + deltax
b = self.winfo_y() + deltay
self.geometry("+%s+%s" % (a, b))
def Main():
app = Application()
app.mainloop()
if __name__ == "__main__":
Main()
Read PEP-8 Install and run one or all of PEP8 checker, pyFlakes, pyChecker, pylint.
The first thing that stands out is that docstrings are supposed to be within the function rather than before it - then they become a part of the function code and can be accessed by help.
Related
I am creating a painting application and I want to save my drawing on canvas widget as png file on my computer. This is my code:
from tkinter import *
from tkinter.filedialog import *
from functools import partial
from tkinter import Menu
from tkinter import filedialog,messagebox
from PIL import Image
from tkinter.colorchooser import askcolor
import pyscreenshot as ImageGrab
import pyautogui
class PaintingApp:
x=y=None
def __init__(self,window):
self.window = window
self.upper_frame = Frame(window)
self.upper_frame.grid(row=0,column=0, padx=10, pady=5,sticky="ew")
self.lower_frame = Frame(window)
self.lower_frame.grid(row=2, column=0, padx=10, pady=5,sticky="ew")
self.canvas= Canvas(self.lower_frame,width=500,height=530,bg="white")
self.canvas.grid()
self.objects = [] #objects on canvas
self.pen_size = 2
self.pcolor = "black"
self.pen = Button(self.upper_frame,text="Pen",command=partial(self.pen_draw,thickness=self.pen_size))
self.pen.grid(row=0,column=3,padx=(10,160))
self.bg = Button(self.upper_frame,text="Background",command= self.bgcolor) #change bg color
self.bg.grid(row=2,column=1,padx=(100,10))
self.upper_menu()
self.canvas.bind("<Button-1>", self.get_x_and_y)
self.canvas.bind("<B1-Motion>", lambda event, b=self.pen_size: self.pen_draw(b,event))
self.im = None
def save_pic(self,event=None):
file = asksaveasfilename(defaultextension=".png")
x = self.canvas.winfo_rootx() + self.canvas.winfo_x()
y = self.canvas.winfo_rooty() + self.canvas.winfo_y()
x1 = x + self.canvas.winfo_width()
y1 = y + self.canvas.winfo_height()
self.im=ImageGrab.grab(bbox=(x,y,x1,y1))
self.im.save(file[19:])
def pen_color(self,color):
self.pcolor= color
def get_x_and_y(self,event):
global x,y
x, y = event.x, event.y
def pen_draw(self,thickness,event=None):
global x,y
self.canvas.bind("<Button-1>", self.get_x_and_y) # Bind to pen_draw function
self.canvas.bind("<B1-Motion>", lambda event, b=self.pen_size: self.pen_draw(b,event))
if event != None:
self.objects.append(self.canvas.create_line((x, y, event.x, event.y), fill=self.pcolor,width=self.pen_size,capstyle=ROUND,smooth=True))
x, y = event.x, event.y
def upper_menu(self):
self.menubar = Menu(window)
self.menu1 = Menu(self.menubar, tearoff=0)
self.menu1.add_command(label="Save pic", command=self.save_pic)
self.menu1.add_separator()
self.menu1.add_command(label="Exit", command=window.destroy)
self.menubar.add_cascade(label="Settings", menu=self.menu1)
self.menu2 = Menu(self.menubar, tearoff=0)
self.menu2.add_command(label="Open pic")
self.menubar.add_cascade(label="Image", menu=self.menu2)
self.window.config(menu=self.menubar)
def bgcolor(self):
chosen_color = askcolor(color=self.canvas["bg"])[1]
self.canvas.configure(bg=chosen_color)
window = Tk()
window.geometry("500x450")
p = PaintingApp(window)
window.mainloop()
Now I have tried many many codes but it won't work. The code I provided above saves an all black picture which does not make any sense. I have tried using the module pyautogui as well but I still get the same result.
def save_pic(self,event=None):
file = asksaveasfilename(defaultextension=".png")
x = self.canvas.winfo_rootx() + self.canvas.winfo_x()
y = self.canvas.winfo_rooty() + self.canvas.winfo_y()
x1 = x + self.canvas.winfo_width()
y1 = y + self.canvas.winfo_height()
self.im=pyautogui.screenshot(region=(x,y,x1,y1))
self.im.save(file[19:])
If you use a screen shot library, you should wait until the backend UI framework(X11 in your case) finishes the drawing. Also you can use the PIL.ImageGrab.grab() in Pillow instead of pyscreenshot.
So do like this.(I fixed several errors in the original example, such as an incorrect path.)
...
from PIL import ImageGrab
...
class PaintingApp:
...
def save_pic(self,event=None):
file = asksaveasfilename(defaultextension=".png")
def grab_and_save():
x = self.canvas.winfo_rootx()
y = self.canvas.winfo_rooty()
x1 = x + self.canvas.winfo_width()
y1 = y + self.canvas.winfo_height()
self.im = ImageGrab.grab(bbox=(x,y,x1,y1))
self.im.save(file)
self.window.update()
self.window.after(1000, grab_and_save) # This waits 1000ms.
...
...
A better method will be to install Tkimg and export the bitmap on the canvas directly, but it will need some hard work. You can start with an unmaintained project, python-tkimg.
So I'm just trying to create a window with a shadow. I want to move both windows at the same time. so far it works. however, when i click on the edge, the "shadow" window comes to the fore. is there a possibility to leave the second window permanently in the background?
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
class CFrame(Tk):
def __init__(self, parent=None, bg="#1b1e21", bd=1, bdcolor="lime"):
super().__init__(parent)
self.bg = bg
self.bd = bd
self.bdcolor = bdcolor
self.title("CFrame")
self.geometry("740x740+200+200")
self.overrideredirect(1)
self.bind("<B1-Motion>", self.dragWindow)
self.bind("<Button-1>", self.clickWindow)
self.shadow = Shadow()
self.headingFrame = Frame(self, bg="lime")
self.headingFrame.pack(fill="x")
self.mainFrame = Frame(self, bg=self.bg, highlightthickness=bd,
highlightbackground=self.bdcolor, highlightcolor=self.bdcolor)
self.mainFrame.pack(fill="both", expand="yes")
self.exitButton = ttk.Button(self.headingFrame, text="\u2613", command=self.programQuit)
self.exitButton.pack(side="right")
self.offSetX = 0
self.offSetY = 0
def dragWindow(self, event):
x = self.winfo_pointerx() - self.offSetX
y = self.winfo_pointery() - self.offSetY
x2 = self.winfo_pointerx() - self.offSetX - 10
y2 = self.winfo_pointery() - self.offSetY - 10
self.geometry("+%d+%d" % (x,y))
self.shadow.geometry("+%d+%d" % (x2,y2))
def clickWindow(self, event):
self.offSetX = event.widget.winfo_rootx() - self.winfo_rootx() + event.x
self.offSetY = event.widget.winfo_rooty() - self.winfo_rooty() + event.y
def programQuit(self):
ex = messagebox.askyesno("Quit?", "Quit?")
if ex == 1:
self.destroy()
class Shadow(Tk):
def __init__(self):
super().__init__()
self.title("Shadow")
self.geometry("760x760+190+190")
self.overrideredirect(1)
self.attributes("-alpha", 0.3)
self.bg = Frame(self, bg="black")
self.bg.pack(fill="both", expand="yes")
if __name__ == '__main__':
cf = CFrame()
cf.mainloop()
maybe a little bit too much code, but i think you can it imagn better with the whole example
I already did something like this here. It works by binding to "<FocusIn>" on your dummy shadow window and calling .focus_force() on the main window. Like this:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
class CFrame(tk.Tk):
def __init__(self, bg="#1b1e21", bd=1, bdcolor="lime"):
super().__init__()
self.bg = bg
self.bd = bd
self.bdcolor = bdcolor
super().geometry("740x740+200+200")
super().overrideredirect(True)
super().bind("<B1-Motion>", self.drag_window)
super().bind("<Button-1>", self.click_window)
self.shadow = Shadow()
self.heading_frame = tk.Frame(self, bg="lime")
self.heading_frame.pack(fill="x")
self.main_frame = tk.Frame(self, bg=self.bg, highlightthickness=bd,
highlightbackground=self.bdcolor,
highlightcolor=self.bdcolor)
self.main_frame.pack(fill="both", expand="yes")
self.exit_button = ttk.Button(self.heading_frame, text="\u2613",
command=self.program_quit)
self.exit_button.pack(side="right")
self.offset_x = 0
self.offset_y = 0
self.shadow.bind("<FocusIn>", self.focus_main)
def focus_main(self, event):
super().focus_force()
def drag_window(self, event):
x = self.winfo_pointerx() - self.offset_x
y = self.winfo_pointery() - self.offset_y
x2 = self.winfo_pointerx() - self.offset_x - 10
y2 = self.winfo_pointery() - self.offset_y - 10
super().geometry("+%d+%d" % (x,y))
self.shadow.geometry("+%d+%d" % (x2,y2))
def click_window(self, event):
self.offset_x = event.widget.winfo_rootx() - self.winfo_rootx() + event.x
self.offset_y = event.widget.winfo_rooty() - self.winfo_rooty() + event.y
def program_quit(self):
result = messagebox.askyesno("Quit?", "Are you sure you want to quit?")
if result:
super().destroy()
self.shadow.destroy()
class Shadow(tk.Tk):
def __init__(self):
super().__init__()
super().geometry("760x760+190+190")
super().overrideredirect(True)
super().attributes("-alpha", 0.3)
super().config(bg="black")
if __name__ == "__main__":
cf = CFrame()
cf.mainloop()
Also I removed the frame from the shadow window and replaced it with <tkinter.Tk>.config(bg="black").
Another thing: Is there a point to calling .title(...) when you are going to use .overrideredirect(True)?
This program works and displays server latency on a small canvas, but because it takes the program time to ping the server and display the ping def display():, it is not possible to drag the window class WindowDraggable():, until the subprocess has finished, and thus there is lag when dragging the window. Can this lag be resolved with mutil-threading so the window can be dragged smoothly?
from tkinter import *
from PIL import ImageTk, Image
import subprocess
import _thread
host = "141.101.115.212" #host IP address
root = Tk()
root.overrideredirect(1)
im = Image.open("image.png")
width, height = im.size
canvas = Canvas(root, width=width, height=height)
canvas.pack()
image = ImageTk.PhotoImage(file="image.png")
canvas.create_image(0, 0, image=image, anchor=NW)
text = canvas.create_text(125, 75, anchor=CENTER)
def display():
global text
#Launches 'command' windowless and waits until finished; finds ping
suinfo = subprocess.STARTUPINFO()
suinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
x = subprocess.Popen(["ping.exe", "141.101.115.212"], stdout=subprocess.PIPE, startupinfo=suinfo)
#find latency with regex
x = str(x.communicate()[0])
lhs, rhs = x.split("Average = ")
lhs, rhs = rhs.split("\\", 1)
lhs, rhs = lhs.split("m")
if int(lhs) > 999:
lhs = "999" + "ms"
latency = lhs
canvas.itemconfig(text, text=latency, width=width)
canvas.itemconfig(text, font=("courier", 25, "bold"))
canvas.itemconfig(text, fill="white")
root.after(1000, display)
class WindowDraggable():
def __init__(self, label):
self.label = label
label.bind('<ButtonPress-1>', self.StartMove)
label.bind('<ButtonRelease-1>', self.StopMove)
label.bind('<B1-Motion>', self.OnMotion)
def StartMove(self, event):
self.x = event.x
self.y = event.y
def StopMove(self, event):
self.x = None
self.y = None
def OnMotion(self,event):
x = (event.x_root - self.x - self.label.winfo_rootx() + self.label.winfo_rootx())
y = (event.y_root - self.y - self.label.winfo_rooty() + self.label.winfo_rooty())
root.geometry("+%s+%s" % (x, y))
label = Label(root, text='drag me')
WindowDraggable(label)
label.pack()
#_thread.start_new_thread( print_time, ("Thread-2", 4, ) )
root.after(0, display())
root.mainloop()
Rather than try to fight Tkinter's builtin loop/threading, use it:
def wait_for_it(proc):
proc.poll()
if proc.returncode is None: # subprocess hasn't finished yet
root.after(100, lambda: wait_for_it(proc)) # register a callback for 100ms
else:
display(proc.communicate()[0])
def display(x):
lhs, rhs = x.split("Average = ")
# the rest of your code goes here...
# instead of root.after(0, display)
wait_for_it(subprocess.Popen(['ping', 'google.com']))
As a slight aside, I highly recommend pasting your code on the Code Review Stack Exchange to get some style pointers and help simplifying it.
How can I drag/move images using pygtk?
Here is a video of someone doing it, however the person did not give the source code.
here's a little demo (i don(t remmeber excatly were it comes from, sorry) which is very similar to the one found here: http://www.pygtk.org/pygtk2tutorial/examples/dragndrop.py
import gtk
class DragImage(gtk.Image):
def __init__(self,image,layout):
gtk.Image.__init__(self)
self.drag = False
self.drag_x = 0
self.drag_y = 0
self.layout = layout
self.x = 0
self.y = 0
self.set_from_file(image)
self.event_box = gtk.EventBox()
self.event_box.set_visible_window(False)
self.event_box.add(self)
self.event_box.add_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK)
self.event_box.connect("button-press-event", self.click)
self.event_box.connect("button-release-event", self.release)
self.event_box.connect("motion-notify-event", self.mousemove)
self.layout.put( self.event_box, 0, 0 )
def click(self, widget, event):
self.drag = True
self.drag_x = event.x
self.drag_y = event.y
print(self.drag_x, self.drag_y)
def release(self, widget, event):
self.drag = False
def mousemove(self,widget,event):
if self.drag:
self.layout.move(self.event_box,self.x+int(event.x-self.drag_x),self.y+int(event.y-self.drag_y))
self.x, self.y = self.layout.child_get(self.event_box,'x','y')
class move_test(object):
def __init__(self):
window = gtk.Window()
layout = gtk.Layout()
img1 = DragImage('image1.jpg',layout)
img2 = DragImage('image2.jpg',layout)
window.add(layout)
window.show_all()
move_test()
gtk.main()
I can't get tooltip to work with my always on top window. Obviously the problem is you can't create something on top of something that's always on top; so i was wondering if there was a workaround or solution. The Popup needs to be always on top of other windows, but i also need to have all the tooltips show up properly.
Here's a stripped down version of what I have so far:
from Tkinter import *
class GUI:
def __init__(self, root):
Popup = Toplevel(root)
Popup.resizable(0,0)
Popup.attributes("-toolwindow", 1)
Popup.wm_attributes("-topmost", 1)
PFrame = Frame(Popup)
self.B = Button(PFrame, width=10,height=10)
self.B.pack()
self.createToolTip(self.B,"Click this button.")
PFrame.pack()
class ToolTip(object):
def __init__(self, widget):
self.widget = widget
self.tipwindow = None
self.id = None
self.x = self.y = 0
def showtip(self, text):
self.text = text
if self.tipwindow or not self.text: return
x,y,cx,cy = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() +15
y = y + cy + self.widget.winfo_rooty() +65
self.tipwindow = tw = Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d"%(x,y))
label = Label(tw, text=self.text, justify=LEFT)
label.pack(ipadx=1)
def hidetip(self):
tw = self.tipwindow
self.tipwindow = None
if tw: tw.destroy()
def createToolTip(self,widget,text):
toolTip = self.ToolTip(widget)
def enter(event): self.tt = root.after(1500,show,event)
def show(event): toolTip.showtip(text)
def leave(event):
if self.tt: root.after_cancel(self.tt)
toolTip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
if __name__ == '__main__':
root = Tk()
App = GUI(root)
root.mainloop()
I fixed it by adding tw.wm_attributes("-topmost", 1) to the showtip function. Let me know if this solution is incorrect or if there is a better way.