need kick-start on Tkinter program

Post here if you need help with creating a Graphical User Interface in Python.

need kick-start on Tkinter program

Postby Stauricus » Mon Nov 18, 2013 9:39 pm

hello everybody
this is my first time trying to create a GUI application in Tkinter. long time after not seeing Python. i missed it.
what i'm trying to do is a simple tile map creator. altough i'm not trying to create the next RPG Maker, i'd want it to somehow look like it:
Image

at first, it would consist of: a menubar, on the top, with its options; a status bar, on the bottom; and two main frames (i think that's the name) with lots of images in a grid (and scrollbars, of course). the left one, the tileset, and the right one, the map.

i have almost nothing so far:
Code: Select all
import sys
from Tkinter import *
import tkMessageBox

tile_size = 64
window_x = tile_size*10 + tile_size*5
window_y = tile_size*10

def newFileOption():
    print 'New File'
def saveFileOption():
    print 'Save File'
def saveFileAsOption():
    print 'Save File'
def loadFileOption():
    print 'Load File'
def exportFileOption():
    print 'Export File'
def exitOption():
    print 'Exit File'
def configOption():
    print 'Configurações'
def aboutOption():
    print 'About...'
    tkMessageBox.showinfo(title='Sobre TileMap Creator', message='TileMap Creator 0.1')

print "window_x:", window_x, "; window_y:", window_y

window = Tk()
window_string = str(window_x) + 'x' + str(window_y)
window.geometry(window_string)
window.resizable(0, 0)
del window_string
window.title('TileMap Creator')
menu_bar = Menu(window)
#File Menu
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label='Novo', command=newFileOption)
file_menu.add_command(label='Salvar', command=saveFileOption)
file_menu.add_command(label='Salvar como...', command=saveFileAsOption)
file_menu.add_command(label='Carregar...', command=loadFileOption)
file_menu.add_command(label='Exportar...', command=exportFileOption)
file_menu.add_command(label='Sair', command=exitOption)
#Options Menu
options_menu = Menu(menu_bar, tearoff=0)
options_menu.add_command(label='Configurações', command=configOption)
#Help Menu
help_menu = Menu(menu_bar, tearoff=0)
help_menu.add_command(label='Sobre...', command=aboutOption)

menu_bar.add_cascade(label='Arquivo', menu=file_menu)
menu_bar.add_cascade(label='Opções', menu=options_menu)
menu_bar.add_cascade(label='Ajuda', menu=help_menu)
window.config(menu=menu_bar)

window.mainloop()


just the bars are there :P
ok, for now, what i want to know is: how do i insert a frame that contains many small clickable images, and is scrolable (as there will be more images than can be showed at once)?
thanks in advance.
Last edited by micseydel on Mon Nov 18, 2013 9:50 pm, edited 1 time in total.
Reason: Locked OP.
Stauricus
 
Posts: 6
Joined: Mon Nov 18, 2013 3:35 pm

Re: need kick-start on Tkinter program

Postby octopez » Tue Nov 19, 2013 10:04 am

Hi,

and welcome to the forums.

I'm not entirely sure, if Tkinter can do that.
First of all, Tkinter has quite limited support for common bitmap-formats. AFAIR jpg and png do not work out of the box. So you also would have to use PIL (Python Imaging Lib). I don't know if PIL is now available for Python 3.x. And I can remember that I had problems with PIL on OSX some time ago (don't know, if this is still true).

I'd suggest using one of the other toolkits, like PySide/PyQt, PyGtk or wxPython. They may seem a little more complicated, but they are capable of the things you need.
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: need kick-start on Tkinter program

Postby Stauricus » Tue Nov 19, 2013 11:10 am

really? i tough it could handle atleast the most common formats...
i'll try one of the others, then.
thanks for our suggestion :)
Stauricus
 
Posts: 6
Joined: Mon Nov 18, 2013 3:35 pm

Re: need kick-start on Tkinter program

Postby octopez » Tue Nov 19, 2013 12:13 pm

Some (long) time ago, I had the idea to make some kind of map making software (for pen & paper rpgs). I've used Python 2.6 and wxPython back then.
As often with such great projects, I failed. :D
Nevertheless - some sources are still availiable:
https://dl.dropboxusercontent.com/u/481 ... apster.zip

Basically it's a window with a side pane and a drawing area. In the side pane you can load SINGLE tiles (no complete tilemap!!!). Then you select one of the tiles and with a left click in the pink drawing area you draw the tiles. Generally it's just a demo, as all the other functionalities are missing. But maybe it can show you how to use wxPython and it's drawing capabilities. ;)
If you have questions, just ask and we'll try to help - I havn't used wxPython the last years and I'm just a humble hobbyist though!
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: need kick-start on Tkinter program

Postby Stauricus » Tue Nov 19, 2013 1:20 pm

well sir, that's EXACTLY what i want.
thank you for sharing your code and for the politeness :D
Stauricus
 
Posts: 6
Joined: Mon Nov 18, 2013 3:35 pm

Re: need kick-start on Tkinter program

Postby Stauricus » Fri Nov 22, 2013 10:54 pm

well, i didn't had much time these days, but for now i have this:

Header.py
Code: Select all
import wx

class MapWindow(wx.Window):
    def __init__(self, parent, id):
        wx.Window.__init__(self, parent, id, style=wx.NO_FULL_REPAINT_ON_RESIZE)
        self.SetBackgroundColour(wx.Colour(255, 200, 255))

class TilesetWindow(wx.Window):
    def __init__(self, parent, id, images, tile_size):
        wx.Window.__init__(self, parent, id, style=wx.RAISED_BORDER)
        self.SetBackgroundColour(wx.Colour(0, 255, 255))
        self.SetCursor(wx.StockCursor(wx.CURSOR_PEN))
       
class TileBook(wx.Notebook):
   def __init__(self, parent, id):
      wx.Notebook.__init__(self, parent, id)
      win = wx.Window(self, wx.ID_ANY, style = wx.SUNKEN_BORDER)
      win2 = wx.Window(self, wx.ID_ANY, style = wx.SUNKEN_BORDER)
      win.SetBackgroundColour(wx.Colour(0, 255, 255))
      win2.SetBackgroundColour(wx.Colour(0, 255, 255))
      self.AddPage(win, "Ground")
      self.AddPage(win2, "Roof")


TileMap.py:
Code: Select all
# -*- coding: utf-8 -*-

import wx
from Header import MapWindow, TilesetWindow, TileBook

class MainWindow(wx.Frame):
   tile_size = 64
   windowsize_x = 800
   windowsize_y = 600
   def __init__(self, parent, title):
      wx.Frame.__init__(self, parent, style=wx.DEFAULT_FRAME_STYLE, title=title, size=(self.windowsize_x, self.windowsize_y))
      
      file_menu=wx.Menu()
      help_menu = wx.Menu()
      file_menu.Append(wx.ID_NEW, "New")
      file_menu.AppendSeparator()
      file_menu.Append(wx.ID_EXIT, "Exit")
      help_menu.Append(wx.ID_ABOUT, "About...")
      menubar = wx.MenuBar()
      menubar.Append(file_menu, "File")
      menubar.Append(help_menu, "Help")
      self.SetMenuBar(menubar)
      
      self.img_list = wx.ImageList(self.tile_size, self.tile_size, True)
      self.splitter = wx.SplitterWindow(self, wx.ID_ANY, style=wx.SP_LIVE_UPDATE)
      self.left_window = TileBook(self.splitter, wx.ID_ANY)
      self.right_window = MapWindow(self.splitter, wx.ID_ANY)
      self.splitter.SetMinimumPaneSize(200)
      self.splitter.SplitVertically(self.left_window, self.right_window, -100)

      self.Show(True)

application = wx.App(False)
window = MainWindow(None, "TileMap Creator")
application.MainLoop()


now, i'm trying to understand what's the use of wx.ImageList and wx.ListCtrl. in the code, they seemed to be used together, but i didn't got why. could i use just an ImageList for showing and selecting the images on the left panel?

thanks in advance!
Stauricus
 
Posts: 6
Joined: Mon Nov 18, 2013 3:35 pm

Re: need kick-start on Tkinter program

Postby octopez » Sun Nov 24, 2013 7:59 pm

Hi, as far as I can remember, you need the ImageList to have a reference for the used images. The ImageListe is not a GUI-element, it's a hidden object that holds the references to the imported/used images and is connected to the ListCtrl.

I recommend to install the wxpython Docs and Demos. There are lots of usefull examples that explain these things pretty well!
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: need kick-start on Tkinter program

Postby Stauricus » Tue Nov 26, 2013 2:47 am

yeah, i tought about that too. but after numerous tries, i gave up. this is the nearest i got:

Code: Select all
      self.img_list = wx.ImageList(tile_size, tile_size, True)
      self.bmp = wx.Bitmap('t.jpg', wx.BITMAP_TYPE_JPEG)
      self.img_list.Add(self.bmp)
      self.tile_list = wx.ListCtrl(self, wx.ID_ANY, style=wx.LC_ICON)
      self.tile_list.AssignImageList(self.img_list, wx.IMAGE_LIST_NORMAL)

but it just shows a blank image, where the icon should be. i believe there's no error loading the image, because when that happens, an error message appears.
now i'm just making this program in C++ and SFML. i'm recreating all gui elements, but atleast things are moving faster.
thanks anyway :)
Stauricus
 
Posts: 6
Joined: Mon Nov 18, 2013 3:35 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests

cron