Two colors in one string - ruby

Hi I'm trying to do a GUI code using GTK in Ruby and I'm stuck trying to change the color of a String.
I would like the Welcome to be blue and the #name to be red but I can't seem to figure out a way to get both of them
#user = Gtk::Label.new("Welcome #{#name}")
css_user = Gtk::CssProvider.new
css_user.load(data: "label{color: blue;}")
If anybody could help I would be really greatful

I had to change a little bit the gtk display
#box = Gtk::Box.new(:horizontal, 1)
#welcome = Gtk::Label.new("Welcome ")
#user = Gtk::Label.new(#usuari)
css_user = Gtk::CssProvider.new
css_user.load(data: "label{color: blue;}")
css_welcome = Gtk::CssProvider.new
css_welcome.load(data: "label{color: black;}")
#user.style_context.add_provider(css_user, Gtk::StyleProvider::PRIORITY_USER)
#welcome.style_context.add_provider(css_welcome, Gtk::StyleProvider::PRIORITY_USER)
As you can see I created two labels (one for each color) and I placed them inside a Horizontal Box

Related

How can i change the character image non Ruby Gosu

im building a basic game in the style of the original pokemon games using the ruby gosu library. Ive managed to figure out how to move the originally loaded sprite about but i cant figure out how to clear that sprite and draw the new sprite e.g. back view in its place.
Ive been looking throught the documentation and came across the "insert" method, although im not sure if this is what i want. Any help?
im creating the var in the initialize method then drawing it later on like so:
def initialize
#character_image = Gosu::Image.new("media/images/char.png", :tileable => false)
end
def draw
#character_image.draw(#character_location_X, #character_location_Y, 1)
end
You need to make a class for your character, which needs an update and draw function. In the update function, when input such as WASD is received you can switch the image of the sprite. If you don't have a sprite sheet, you'll have to load multiple images and switch between them.
Here's some ruby pseudocode to help you:
#back_image = Gosu::Image.new("media/images/back.png")
#front_image = Gosu::Image.new("media/images/front.png")
#left_image = Gosu::Image.new("media/images/left.png")
#right_image = Gosu::Image.new("media/images/right.png")
current_image = front_image
This goes in your update function:
if up
current_image = back_image
elsif down
current_image = front_image
elsif right
current_image = right_image
elsif left
current_image = left_image
end
Then in your draw function all you need to do is
def draw
#current_image.draw(#character_location_X, #character_location_Y, 1)
end
This is a pretty basic way, but if you use a sprite sheet, you can create your own animation class that Gosu can use that allows you to select between certain ranges of frames of your character spritesheet.

How to handle sprite animation in Kivy

I am coding a game using Kivy. I have a Screen class where I put my animation code. It's not a usual game, it's more like several screens, each with its own animation, with button commands for going back and forth to different screens.
It works ok, but when I make more classes like this and put it all in a ScreenManager, the animation is disrupted with random white screens.
class Pas(Screen):
def __init__(self, **kwargs):
super(Pas, self).__init__(**kwargs)
Clock.schedule_interval(self.update, 1 / 60.0)
self.ani_speed_init = 15
self.ani_speed = self.ani_speed_init
self.ani = glob.glob("img/pas_ani*.png")
self.ani.sort()
self.ani_pos = 0
self.ani_max = len(self.ani)-1
self.img = self.ani[0]
self.update(1)
back = Button(
background_normal=('img/back-icon.png'),
background_down=('img/back-icon.png'),
pos=(380, 420))
self.add_widget(back)
def callback(instance):
sm.current = 'game'
back.bind(on_press=callback)
def update(self, dt):
self.ani_speed -= 1
if self.ani_speed == 0:
self.img = self.ani[self.ani_pos]
self.ani_speed = self.ani_speed_init
if self.ani_pos == self.ani_max:
self.ani_pos = 0
else:
self.ani_pos += 1
with self.canvas:
image = Image(source=self.img, pos=(0, 0), size=(320, 480))
What am I doing wrong? I am also accepting ideas for a different way of doing this.
If you want to use Screen and ScreenManager for your screens, it would be better to use the transition system they define and use, so, to define your own Transitions, and apply them. If you want more control, i would advise getting ride of Screen and ScreenManager, and just using Widgets, to control the whole drawing/positioning process.
Also, Clock.schedule_interval(self.update, 0) is equivalent to the call you are making, the animation will be called each frame, and you can use dt to manage the animation progress.
Also, kivy can manage gifs, as well as zip archives of images to directly do animations (useful to have animated pngs), you can let kivy manage the whole animation process this way.

Dynamic refresh in a view - xcode 4 / interface builder

I'm new to xcode so please bear with me.
I have a label that I have set to blank, and after a user clicks 'go' I generate a random word or number and use:
self.label.stringValue = "some_word"
to update the view. (I am using MacRuby btw)
However, I would like to show 20 or so random words in quick succession before the last one is shown - just because it's too boring at the moment. (Alternatively, I'd be happy with showing an animated graphic in its place - which is replaced by the final random word.)
I've tried things like:
100.times do
num = rand(40)
self.label.stringValue = num
end
But it doesn't work. I've also tried .reloadData but to no avail as well.
Any ideas on how to achieve this?
So as not to leave the question haning, from the Macruby mailing list:
def drawWord(sender)
if !next_word
self.timer.invalidate
return
end
self.label.stringValue = next_word
self.setNeedsDisplay true
end
def next_word
...
end
self.timer = NSTimer.scheduledTimerWithTimeInterval( 1/20.0,
target:self, selector:"drawWord:", userInfo:nil, repeats:true)

wxOGL wx.ShapeCanvas images

I am making a small photo editing application using wxPython ogl.ShapeCanvas. I can load images on the canvas. I just want to know how will I adjust the brightness/contrast of an image inside the canvas (using a slider).
Thanks
Hope it's not to late to be of any help, but...I had to do something similar with OGL recently when I wanted to adjust the transparency on the fly. What I ended up doing was creating a class that made the adjustments and returned a wx.Bitmap, then I had a ShapeCanvas subclass use the adjusted picture, e.g.
class PicAdjuster(cls):
def adjust_pic(self, image_filename, factor_red = 1., factor_green = 1., factor_blue = 1., factor_alpha = 1.):
original_img = wx.Image(image_filename)
adjusted_img = original_img.AdjustChannels(factor_red, factor_green, factor_blue, factor_alpha)
return wx.BitmapFromImage(adjusted_img)
then for the ShapeCanvas:
class PicDisplay(ogl.ShapeCanvas):
def add_picture(self, image_filename):
new_img = ogl.BitmapShape()
add_alpha = PicAdjuster()
new_img.SetBitmap(add_alpha.adjust_pic(factor_alpha = 0.5))
self.diagram.AddShape(new_img)
Anyway, you might be able to do something similar to make your adjustments; just use your picture adjuster and call the ogl.BitmapShape's SetBitmap() method as required.

Python 3.1 Tkinter layout help. I am close, please help me finish this

I am using Python 3.1 by the way.
I am trying to build a simple GUI using Tkinter - label, text entry field, button on the first row and editable text area with scrollbar to the right and on the bottom of it - on the second row. Please help me fix up the layout. What I have below does not quite work. If I have to use a grid, I will. I wish to keep the code very simple - I want to "sell" Python to some of my coworkers. So, I want to get a somewhat decent look and feel. Suggest better padding if you do not mind. Also, if my variable names, etc. seem weird, then please make a note.
At the same time I want to pretend that this is a throw-away script which I have not spent much time on. Since I am asking for your help, it ain't so, but they do not need to know ;). So, I do not want to introduce fancy code to create nice borders, etc. I just want something that is visually appealing, clean and simple. If I do not, then my presentation will not achieve its goal.
Thank you, my code is below:
class App:
def __init__(self, parent):
frame = Frame(parent)
self.__setup_gui(frame) # Call Helper
frame.pack(padx=15, pady=15)
parent.title('To be changed')
def __setup_gui(self, frame):
# First Row
self.cs_label = Label(frame, text='Change Set: ')
self.cs_label.pack(side=LEFT, padx=10, pady=10)
self.cs_val = Entry(frame, width=10)
self.cs_val.pack(side=LEFT, padx=10, pady=10)
self.get_button = Button(frame, text='Get', command=self.get_content)
self.get_button.pack(side=LEFT, padx=10, pady=10)
# Text area and scrollbar
self.text_area = Text(frame, height=10, width=50, background='white')
# Put a scroll bar in the frame
scroll = Scrollbar(frame)
self.text_area.configure(yscrollcommand=scroll.set)
self.text_area.pack(side=TOP)
scroll.pack(side=RIGHT,fill=Y)
self.clipboard_var = IntVar()
self.notepad_var = IntVar()
def get_content(self):
print(self.clipboard_var.get())
print(self.notepad_var.get())
###################################################################################################
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
You definitely want the grid manager -- Pack only works for a vertical or horizontal stackup by itself. You can use multiple frames to work around it, but I find it's easier to expand a GUI if you just do it with Grid to start.
Here's what I've worked up real quick based what you said and the code. I reduced/removed the padding -- it looked huge for me -- and I set up two scrollbars, in a subframe to make the padding work out more easily. Note that to make the horizontal scrollbar useful your Text area needs to have wrap=NONE; otherwise you might as well use the easy 'ScrolledText' widget from tkinter.scrolledtext and skip the horizontal scroll bar.
I've now reframed things a bit to allow for resize, with a minimum size that shows the top buttons -- see the uses of minsize and row/columnconfigure.
BTW, it looks like your variables aren't being pulled from anywhere -- is that intentional?
from tkinter import *
class App:
def __init__(self, parent):
self.__setup_gui(parent) # Call Helper
parent.title('To be changed')
def __setup_gui(self, parent):
# First Row
self.rowframe = Frame(parent)
self.rowframe.grid()
self.cs_label = Label(self.rowframe, text='Change Set: ')
self.cs_label.grid(row=0, column=0, padx=2, pady=2)
self.cs_val = Entry(self.rowframe, width=10)
self.cs_val.grid(row=0, column=1, padx=2, pady=2)
self.get_button = Button(self.rowframe, text='Get', command=self.get_content)
self.get_button.grid(row=0, column=2, padx=2, pady=2)
parent.update_idletasks()
parent.minsize(width=self.rowframe.winfo_width(), height=self.rowframe.winfo_height())
# Text area and scrollbars
self.textframe = Frame(parent)
self.textframe.grid(row=1, columnspan=2, padx=2, pady=2, sticky=N+S+E+W)
self.hscroll = Scrollbar(self.textframe, orient=HORIZONTAL)
self.vscroll = Scrollbar(self.textframe)
self.text_area = Text(self.textframe, height=10, width=50, wrap=NONE, background='white', yscrollcommand=self.vscroll.set, xscrollcommand=self.hscroll.set)
self.text_area.grid(row=0, column=0, sticky=N+S+E+W)
self.hscroll.config(command=self.text_area.xview)
self.hscroll.grid(row=1, column=0, sticky=E+W)
self.vscroll.config(command=self.text_area.yview)
self.vscroll.grid(row=0, column=1, sticky=N+S)
# Row 0 defaults to 0
parent.rowconfigure(1, weight=1)
parent.columnconfigure(1, weight=1)
# Textarea setup
self.textframe.rowconfigure(0, weight=1)
self.textframe.columnconfigure(0, weight=1)
self.clipboard_var = IntVar()
self.notepad_var = IntVar()
def get_content(self):
print(self.clipboard_var.get())
print(self.notepad_var.get())
###################################################################################################
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
Now, all that said...you might get more visual appeal with PyGTK, PyQt, or wxPython, though tkinter coming "standard" is a nice feature.

Resources