如何在Swift中设置UILabel的textColor(How to set textColor of UILabel in Swift)
当我尝试使用代码将UILabel的颜色设置为另一个UILabel的颜色
myLabel.textColor = otherLabel.textColor
它不会改变颜色。 然而,当我使用这个代码时,
myLabel.textColor = UIColor.redColor()
它正确地改变颜色。 第一行有什么问题?
When I try setting the color of a UILabel to the color of another UILabel using the code
myLabel.textColor = otherLabel.textColor
It doesn't change the color. When I use this code, however,
myLabel.textColor = UIColor.redColor()
It changes the color correctly. What's the issue with the first line?
原文:https://stackoverflow.com/questions/25837228
最满意答案
我希望我的问题是正确的。 你必须在每一步都检查球员的位置。 我建议您在每次移动时定义区域(例如
go_right
),您需要检查玩家是否在这些边界内。 如果是True
,则可以加载另一个脚本。 这是一个快速而肮脏的解决方案:def go_fwd(event): areal.focus_set() me.configure(me.x, me.y-2) check_if_player_enters_region(me) def go_bwd(event): areal.focus_set() me.configure(me.x, me.y+2) check_if_player_enters_region(me) def go_left(event): areal.focus_set() me.configure(me.x-2, me.y) check_if_player_enters_region(me) def go_right(event): areal.focus_set() me.configure(me.x+2, me.y) check_if_player_enters_region(me) def check_if_player_enters_region(player): ## Define your Areas here if(is_in_region(player, 20, 20, 10, 10)): print "enter region 1, load script xxx" if(is_in_region(player, 50, 50, 10, 10)): print "enter region 2, load script xxx" playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line def is_in_region(player, x, y, width, height): # define region boundries if((player.x >= x and player.x <= x+width) and ## check x boundries (player.y >= y and player.y-2 <= y+height)): ## check y boundries return True return False dw = tk.tk() dw.title('Erebos') me = player(0,0) areal = tk.Frame(master=dw, width=20000, height=600, bg='black') areal.pack_propagate(0) areal.pack(fill=tk.BOTH, expand=bool(dw)-100) areal.bind("<1>", lambda event: areal.focus_set()) dw.bind("<Up>", go_fwd) dw.bind("<Down>", go_bwd) dw.bind("<Left>", go_left) dw.bind("<Right>", go_right) playarea = tk.Canvas(width=800, height=500, master=areal, bg='white') playarea.pack() playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line me.zeichne(playarea) dw.mainloop()
我只检查播放器的中心,通常你会检查播放器的“盒子”。
I hope i get your question right. You have to check the players position on every move. I recommend that you define areas, on every move (e.g.
go_right
) you need to check if the player is in those boundaries. If that isTrue
you can load another script. Here is a quick and dirty solution:def go_fwd(event): areal.focus_set() me.configure(me.x, me.y-2) check_if_player_enters_region(me) def go_bwd(event): areal.focus_set() me.configure(me.x, me.y+2) check_if_player_enters_region(me) def go_left(event): areal.focus_set() me.configure(me.x-2, me.y) check_if_player_enters_region(me) def go_right(event): areal.focus_set() me.configure(me.x+2, me.y) check_if_player_enters_region(me) def check_if_player_enters_region(player): ## Define your Areas here if(is_in_region(player, 20, 20, 10, 10)): print "enter region 1, load script xxx" if(is_in_region(player, 50, 50, 10, 10)): print "enter region 2, load script xxx" playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line def is_in_region(player, x, y, width, height): # define region boundries if((player.x >= x and player.x <= x+width) and ## check x boundries (player.y >= y and player.y-2 <= y+height)): ## check y boundries return True return False dw = tk.tk() dw.title('Erebos') me = player(0,0) areal = tk.Frame(master=dw, width=20000, height=600, bg='black') areal.pack_propagate(0) areal.pack(fill=tk.BOTH, expand=bool(dw)-100) areal.bind("<1>", lambda event: areal.focus_set()) dw.bind("<Up>", go_fwd) dw.bind("<Down>", go_bwd) dw.bind("<Left>", go_left) dw.bind("<Right>", go_right) playarea = tk.Canvas(width=800, height=500, master=areal, bg='white') playarea.pack() playarea.create_rectangle(20,20,30,30, fill='blue') ## this is just for visualisation, delete this line playarea.create_rectangle(50,50,60,60, fill='blue') ## this is just for visualisation, delete this line me.zeichne(playarea) dw.mainloop()
I only check the center of the player, normally you do check the "box" of the player.
相关问答
更多-
假设你的意思是你的应用程序窗口,当你说“我的其他窗口”时,你可以在Toplevel或Tk上使用lift()方法: root.lift() 如果您希望窗口保持在所有其他窗口之上,请使用: root.attributes("-topmost", True) root是你的Toplevel还是Tk。 不要忘记- "topmost" ! 要使其成为临时状态 ,请在以下位置禁用最上层: def raise_above_all(window): window.attributes('-topmost', 1 ...
-
有几个选项可以提供给Label小部件,如您在此处所看到的 。 您应该在名称后面指定所有参数: partNumberInfo = tkinter.Label(window, text=PartNumber) 要在Tkinter中使用Python变量,需要特殊的Tk对象,其中有四个: BooleanVar , DoubleVar , IntVar和StringVar 。 看到这个答案是一个很好的例子。 There are several options you can give to a Label widg ...
-
在tkinter窗口中继续检查变量状态的最佳方法(Best method to keep checking the state of a variable in tkinter window)[2019-11-17]
我希望我的问题是正确的。 你必须在每一步都检查球员的位置。 我建议您在每次移动时定义区域(例如go_right ),您需要检查玩家是否在这些边界内。 如果是True ,则可以加载另一个脚本。 这是一个快速而肮脏的解决方案: def go_fwd(event): areal.focus_set() me.configure(me.x, me.y-2) check_if_player_enters_region(me) def go_bwd(event): areal.focu ... -
更改Checkbutton状态时,tkinter变量不会更改(tkinter variable does not change when changing Checkbutton state)[2023-09-17]
你不能有两个Tk实例。 对于第二个窗口,您需要创建一个Toplevel 。 您也不应该在整个程序中多次调用mainloop 。 You can't have two instances of Tk. For your second window you need to create a Toplevel. You also should never call mainloop more than once in your entire program. -
由于递归引起的崩溃可能是因为你的on_resize方法创建了新的小部件。 这是发生了什么: 小部件获得一个...... 调用on_resize ...... 创建一个嵌套循环...... 创建一个画布,其中...... 导致窗口小部件中的配置更改 你在嵌套循环中调用update ... 导致事件被处理,... 调用on_resize,... 创建一个嵌套循环...... 创建一个画布...... 导致窗口小部件中的配置更改 你在嵌套循环中调用update ... 导致事件被处理,... 调用on_resiz ...
-
您可以使用grid来实现这一点,方法是定义放置小部件的row : from tkinter import Tk, Canvas, Button CHANGED = False def options(): button1 = Button(window, text='Click to change door') button1.bind("
", change) button1.grid(row=0, sticky='w') button2 = Butt ... -
tkinter变量跟踪方法(tkinter variable trace method)[2022-11-20]
当Entrybox_002函数返回时, stringvar1被垃圾收集(删除)。 解决方法: stringvar1 = window2.stringvar1 = tk.StringVar(value='asdf') 我建议您将Entrybox_002编码为类,并将stringvar1编码为实例属性以保持参考。 stringvar1 is garbage collected (deleted) when Entrybox_002 function return. Workaround: stringvar1 ... -
在mainloop运行后,你的代码是正确的。 它确实如此,但只有在GUI被销毁之后。 Tkinter设计用于调用mainloop是最后一行(或几乎最后一行)的可执行代码。 一旦调用,所有其他工作必须作为对事件的反应。 这是GUI编程的本质。 “如何更改变量”的答案很简单:在调用mainloop之前执行此mainloop ,或者在响应事件时执行此操作。 例如,在回调按钮时执行此操作,在绑定到事件的函数中执行此操作,或通过after ,依此类推。 You are correct that code after ...
-
您应该将Label(s)放在与您有按钮相同的Frame内。 当我复制你的代码时,我还必须导入filedalog模块( from tkinter import filedalog ),星号导入似乎没有涵盖它。 您可以使用StringVar()变量(来自tkinter)并将其分配给标签。 这个变量你可以从.set()和.get()值。 创建变量并将其分配给Label: self.value = StringVar('', value=" Load GCODE file to find volume, \n w ...
-
您无法获取列表变量,因为窗口小部件对列表变量一无所知。 创建窗口小部件时,您将从列表窗口小部件中提供值 ,但不是列表本身。 menu属性返回Menu类的实例。 它也不会对你的清单有任何了解。 如果要将列表与窗口小部件关联,请将其设置为属性: my_opmenu.list = list1 之后,当您想要列表时,您可以执行以下操作: print "my list:", my_opmenu.list You cannot get the list variable because the widget kno ...