Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何制作tkinter小部件;。放置();python中的方法的工作方式类似于";。setbounds();Java中的方法?_Java_Python_Tkinter - Fatal编程技术网

如何制作tkinter小部件;。放置();python中的方法的工作方式类似于";。setbounds();Java中的方法?

如何制作tkinter小部件;。放置();python中的方法的工作方式类似于";。setbounds();Java中的方法?,java,python,tkinter,Java,Python,Tkinter,Python3.4和EclipseIDE 我有一个java表单正在翻译成python tkinter。我有许多jlabel、jtextfields、jcombobox和一行分隔符,它们需要与各自的python小部件放在相同的python表单上 使用特定定位的java标签示例: JLabel lblWhichTrip = new JLabel("Which Trip:"); lblWhichTrip.setHorizontalAlignment(SwingConstants.RIG

Python3.4和EclipseIDE

我有一个java表单正在翻译成python tkinter。我有许多jlabel、jtextfields、jcombobox和一行分隔符,它们需要与各自的python小部件放在相同的python表单上

使用特定定位的java标签示例:

    JLabel lblWhichTrip = new JLabel("Which Trip:");
    lblWhichTrip.setHorizontalAlignment(SwingConstants.RIGHT);
    lblWhichTrip.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblWhichTrip.setBounds(64, 22, 135, 20);
    frmBookCalc.getContentPane().add(lblWhichTrip);
在50个奇怪的浏览器选项卡之后,python的等价物应该是:

    self.lblWhichTrip = Label(self, text = "Which Trip:", bg = "white", anchor = "e")
    self.lblWhichTrip.place(x = 64, y = 22, height=135, width=20)
但是小部件根本不会出现,甚至不会出现在错误的位置

我想在python中使用“.place()”方法的原因与我在java中使用特定定位的原因相同,人们对“.pack()”方法几乎没有精确的控制

在一个例子中,我发现在“.place()”方法之前直接调用了“.pack()”方法,但更多的例子只使用了“.place()”

问题是:

如何利用“.place()”方法将python tkinter小部件类似地放置在python表单上,就像我对Java表单所做的那样?我知道我很接近,但它不起作用

随着更多的工作,我发现下面的按钮将在我的表格上放置一个按钮,但标签部分如上所述是空白的

    languages = ['Python','Perl','C++','Java','Tcl/Tk']
    labels = range(5)
    for i in range(5):
        ct = [random.randrange(256) for x in range(3)]
        brightness = int(round(0.299*ct[0] + 0.587*ct[1] + 0.114*ct[2]))
        ct_hex = "%02x%02x%02x" % tuple(ct)
        bg_colour = '#' + "".join(ct_hex)
        l = Label(self, text=languages[i], fg='White' if brightness < 120 else 'Black', bg=bg_colour)
        l.place(x = 20, y = 30 + i*30, width=120, height=25)

     self.QUIT = Button(self)
     self.QUIT["text"] = "QUIT"
     self.QUIT["fg"]   = "red"
     self.QUIT["command"] =  self.quit
     self.QUIT.pack({"side": "left"})
我抛弃了py文件和包含所有表单内容的类,使用直接代码创建了一个文件,现在标签不再可见。上面的代码是我目前程序的全部。我希望我能很快解决这个问题

更新:

import tkinter as tk
import random

root = tk.Tk()

w = 465
h = 590        
x = (root.winfo_screenwidth()/2) - (w/2)
y = (root.winfo_screenheight()/2) - (h/2)
root.minsize(width=w, height=h)
root.maxsize(width=w, height=h)
root.resizable(width=tk.FALSE, height=tk.FALSE)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.configure(background='white')

root.option_add("*font", "Tahoma 12")
root.title("Book Calc")

lblWhichTrip = tk.Label(root, text = "Which Trip:", bg = "white", anchor = "e")
lblWhichTrip.place(x=200, y=30, height=135, width=20)

root.mainloop()
root = tk.Tk()
app = Application(root)
高度和宽度需要在适当的位置,而不是标签上。以上代码已修改

lblWhichTrip = tk.Label(root, text = "Which Trip:", bg = "white", anchor = "e")
lblWhichTrip.place(x=200, y=30, height=135, width=20)
这在类结构之外工作得非常好,但在类内部似乎不起作用。这是我上面关于在python表单的特定位置放置标签或其他小部件的问题的一半答案

问题的另一半仍然没有得到回答,因为这个解决方案在类中不起作用,而它应该在类中起作用。如果有人能解释一下,那就太好了。此时,其他人会希望编写一个比我更健壮的程序,因此需要一个比这个更好的答案

多谢各位

更新:

import tkinter as tk
import random

root = tk.Tk()

w = 465
h = 590        
x = (root.winfo_screenwidth()/2) - (w/2)
y = (root.winfo_screenheight()/2) - (h/2)
root.minsize(width=w, height=h)
root.maxsize(width=w, height=h)
root.resizable(width=tk.FALSE, height=tk.FALSE)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.configure(background='white')

root.option_add("*font", "Tahoma 12")
root.title("Book Calc")

lblWhichTrip = tk.Label(root, text = "Which Trip:", bg = "white", anchor = "e")
lblWhichTrip.place(x=200, y=30, height=135, width=20)

root.mainloop()
root = tk.Tk()
app = Application(root)
上面是创建类的时候。Root需要直接发送

def __init__(self, master):
   self.parent = master
需要将指向根的指针设置为类变量以实现持久性

lblWhichTrip = tk.Label(root, text = "Which Trip:", bg = "white", anchor = "e")
lblWhichTrip.place(x=85, y=-35, height=135, width=100)
然后,您可以在一个类中创建所有想要的小部件,所有布局引擎都可以正常工作。包装、放置和网格将完美工作

充分回答了尽职调查的问题

参考:

你提到了
place()
pack()
。。。你查过网格()了吗?我也查不到。请在我添加更多内容时重新评估这个问题。其他更新:“.place()”不喜欢在类中,或者我不知道如何在类中实例化它。我通过忽略我想要使用的类结构来实现它。如何让“.place()”在类中工作?“.place()不喜欢在类中工作”是完全错误的,没有任何意义。不管它是否在课堂上,你现在真正的问题是什么?有一大堆问题和更新。你到底在问什么?你应该点击绿色的勾号来接受你的答案。