| | |
Python GUI Programming
![]() |
2
#71 Oct 13th, 2009
Creating a simple shadow effect around a Tkinter frame ...
python Syntax (Toggle Plain Text)
# create a frame with a shadow effect border # using overlapping frames # vegaseat try: # Python2 import Tkinter as tk except ImportError: # Python3 import tkinter as tk root = tk.Tk() w = 320 h = 220 x = 150 y = 100 # use width x height + x_offset + y_offset (no spaces!) root.geometry("%dx%d+%d+%d" % (w, h, x, y)) root.title("shadow effect border") frame1 = tk.Frame(root, bg='grey', width=w-30, height=h-30) frame1.place(x=20, y=20) frame2 = tk.Frame(root, bg='yellow', width=w-30, height=h-30) frame2.place(x=10, y=10) root.mainloop()
May 'the Google' be with you!
2
#72 Oct 14th, 2009
Ah, the ever so popular Tkinter GUI toolkit. This is the code to get size (width x height) and position (x + y coordinates of the upper left corner of the window in the display screen) information of the Tkinter root window ...
python Syntax (Toggle Plain Text)
# show width and height of Tkinter's root window # vegaseat try: # Python2 import Tkinter as tk except ImportError: # Python3 import tkinter as tk root = tk.Tk() w = 460 h = 320 x = 150 y = 100 # use width x height + x_offset + y_offset (no spaces!) root.geometry("%dx%d+%d+%d" % (w, h, x, y)) # update is needed root.update() geo = root.geometry() print(geo) # 460x320+150+100 width = root.winfo_width() # 460 print(width) height = root.winfo_height() # 320 print(height) root.mainloop()
Last edited by vegaseat; Oct 14th, 2009 at 5:18 pm.
May 'the Google' be with you!
1
#73 Oct 14th, 2009
Here is typical code that I use with the py2exe module to convert Tkinter programs to an executable package for Windows computers ...
Should work with Python26 if you installed the proper py2exe module. Since Python26 uses a different C compiler, msvcr71.dll changes to msvcr90.dll and of course the interpreter is Python26.dll.
python Syntax (Toggle Plain Text)
""" Simple py2exe version used for Tkinter programs. Run this program to create a windows exe file with py2exe. Run it in a folder that als contains your source code file. It will create 2 folders named build and dist. The build folder is temporary info and can be deleted. Distribute whatever is in the dist folder. Your library.zip file contains your optimized byte code, and all needed modules. This file together with the Python interpreter (eg. Python25.dll) should accompany the created executable file. Note that you might be able to share this large library file with other Python/Tkinter based .exe files. The MSVCR71.dll can be distributed and is often already in the Windows/system32 folder. w9xpopen.exe is needed for os.popen() only, can be deleted otherwise. """ from distutils.core import setup import py2exe import sys sys.argv.append("py2exe") sys.argv.append("-q") # insert your own source code filename below ... code_file = 'Tk_ParResist1.pyw' # replace windows with console for a console program setup(windows = [{"script": code_file}])
Last edited by vegaseat; Oct 14th, 2009 at 5:54 pm.
May 'the Google' be with you!
2
#74 30 Days Ago
One way to display a colorful message with the ever so popular PyQT GUI toolkit (works with Python2 and Python3) ...
python Syntax (Toggle Plain Text)
# show a colorful splash message for a set time then quit # tested with PyQT45 by vegaseat from PyQt4.QtCore import * from PyQt4.QtGui import * def do_html(text, color='black', size=3): """create a line of html code for text with color and size""" sf = "<FONT color=%s size=%d>%s</FONT>" return sf % (color, size, text) # build up simple HTML code ... # text between <B> and </B> is bold # <BR> inserts a line break (new line) html = "" html += '<BR>' orange = "#FFBA00" # gives a better orange color color_list = ['red', orange, 'yellow', 'green', 'blue'] ix = 0 for c in 'WE LOVE RAINBOWS': # make character c bold c = "<B>" + c + "</B>" if ix >= len(color_list): ix = 0 html += do_html(c, color_list[ix], 7) ix += 1 html += '<BR>' # create a QT GUI application app = QApplication([]) label = QLabel(html) # show label without frame # comment line out if you want a frame label.setWindowFlags(Qt.SplashScreen) label.show() # show for x milliseconds, then quit x = 5000 QTimer.singleShot(x, app.quit) app.exec_()
May 'the Google' be with you!
2
#75 22 Days Ago
The Text widget of the Tkinter GUI toolkit is the start of a simple text editor. Here we explore how to get the line (row) and column numbers of the text insertion point ...
python Syntax (Toggle Plain Text)
# explore Tkinter's Text widget # get the line and column values of the text insertion point # vegaseat import Tkinter as tk def get_position(event): """get the line and column number of the text insertion point""" line, column = text.index('insert').split('.') s = "line=%s column=%s" % (line, column) root.title(s) root= tk.Tk() root.title('start typing') text = tk.Text(root, width=40, height=10) text.bind("<KeyRelease>", get_position) text.pack() # start cursor in text area text.focus() root.mainloop()
May 'the Google' be with you!
0
#76 2 Days Ago
Here is modified (also added needed delay) and commented code from the pygame tutorial that shows how to load one image onto rectangle object and move the object:
The image used here is attached below, just download it into your working directory. Also click on Toggle Plain Text, select code and copy it to your IDE's editor, save it to the same working directory your image is in, then run it from the IDE. I use PyScripter (Windows) IDE, but you can use IDLE or something.
python Syntax (Toggle Plain Text)
# use pygame to bounce/move loaded image around the screen import sys, pygame pygame.init() size = width, height = 320, 240 # this determines the move direction/speed in pixels # speed[0] left or right # speed[1] up or down speed = [2, 2] # color uses r, g, b tuple black = 0, 0, 0 # white works better here white = 255, 255, 255 # create the window screen = pygame.display.set_mode(size) # give the window a title pygame.display.set_caption("watch the beach ball bounce") # make sure the image file is in the working directory # or give full path name ball = pygame.image.load("ball.bmp") # create rectangle object you can move # it is used to put the image on ballrect = ball.get_rect() while True: for event in pygame.event.get(): # window corner x will exit if event.type == pygame.QUIT: sys.exit() # move the rectangle object ballrect = ballrect.move(speed) # change direction if left or right boundry is reached if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] # change direction if top or bottom boundry is reached if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] # clear the screen with white background screen.fill(white) # put the image on the rectangle object screen.blit(ball, ballrect) # update screen pygame.display.flip() # small time delay milliseconds = 20 pygame.time.delay(milliseconds)
Last edited by bumsfeld; 2 Days Ago at 4:45 pm. Reason: IDE
Should you find Irony, you can keep her!
![]() |
Similar Threads
- Starting wxPython (GUI code) (Python)
- Starting Python (Python)
- Starting with GUI programming? (Python)
- python script help (Python)
- Python GUI Problem (Python)
- C Software Programming.. (C++)
- Frequently Used Python Commands (Python)
- starting Python (Python)
Other Threads in the Python Forum
- Previous Thread: Python - Using a text file to feed into a python dictionary
- Next Thread: pygame problem
| Thread Tools | Search this Thread |
alarm ansi app assignment avogadro backend beginner binary bluetooth character cipher cmd customdialog cx-freeze data decimals dictionary directory dynamic error exe file float format function generator getvalue gnu graphics halp heads homework http ideas import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer prime programming progressbar push py2exe pygame python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver sum text thread threading time tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia write wxpython xlib






