如何以正确的方式放置四个单选按钮(0,0)& (1,0)在python中?

时间:2017-07-17 16:00:10

标签: python python-2.7 tkinter

在我的程序中,我在第2帧中分别使用了4个单选按钮和两个按钮。但我想在(0,0)位置显示r1& r2,在(1,0)位置显示R3& r4纽扣。如果我使用" grid"方法没有以适当的方式显示。

import serial
import Tkinter
from Tkinter import *
import tkMessageBox
class SerialViewer:
def __init__(self):
    self.win = Tk()
    self.ser = serial.Serial('com8',9600)

def bt1 (self):
    self.ser.write('on')

def bt2 (self):
    self.ser.write('off')

def bt3 (self):
    self.ser.write(self.v.get())

def bt5 (self):
    self.ser.write(self.v2.get())


def dialog(self):
    self.index = self.listbox.curselection()[0]
    #self.seltext = self.listbox.get(self.index)
    if self.index == '2':
        tkMessageBox.showinfo('CAUTION!','Run the PSP MODULE first!')
        print(self.listbox.get(self.listbox.curselection()))

    if self.index == '1':
        self.var1 = tkMessageBox.askyesno('Message', 'BYPASS relay on?')
        if self.var1 == 1:
            tkMessageBox.showinfo('CAUTION!','Turning on BYPASS relay')
        else:
            tkMessageBox.showwarning('FAULT!','No UPS found. please turn on 
    bypass relay.')
    if self.index == '0':
        self.var2 = tkMessageBox.askyesno('Message', 'You can only select 
    ADVANCE AUTO mode,Continue?')
        if self.var2 == 1:
            tkMessageBox.showinfo('Operation Mode','Turning on ADVANCE AUTO 
    Mode,')
        else:
            tkMessageBox.showwarning('FAULT!','No operation Mode selected')
    print(self.index)

def makewindow (self):
    frame1 = Frame(self.win)
    frame1.pack()
    b1 = Button(frame1, text = "ON", command = self.bt1)
    b2 = Button(frame1, text = "OFF", command = self.bt2)
    b1.grid(row = 0, column = 0)
    b2.grid(row = 1, column = 1)

    frame2 = Frame(self.win)
    frame2.pack(side = LEFT)
    self.v = StringVar()
    self.text = Label(frame2, text = '# main  power relay:')
    self.text.pack()
    r1 = Radiobutton(frame2,text = 'on', variable = self.v, value = 'on')
    r2 = Radiobutton(frame2,text = 'off', variable = self.v, value = 'off')
    r1.select()
    b3 = Button(frame2, text = 'send', command = self.bt3)
    b3.pack(side = RIGHT, padx = 5)
    r1.pack(side = LEFT)
    r2.pack(side = LEFT)

    self.v2 = StringVar()
    self.text2 = Label(frame2, text = '# main UPS connect:')
    self.text2.pack()
    r3 = Radiobutton(frame2,text = 'on', variable = self.v2, value = 'on')
    r4 = Radiobutton(frame2,text = 'off', variable = self.v2, value = 'off')
    r3.select()
    b5 = Button(frame2, text = 'send', command = self.bt5)
    b5.pack(side = RIGHT, padx = 5,pady = 30)
    r3.pack(side = LEFT,pady = 30)
    r4.pack(side = LEFT,pady = 30)


    frame3 = Frame(self.win)
    frame3.pack()
    self.d = StringVar()
    self.d.set('default')
    label = Label(frame3, textvariable = self.d, relief = RAISED)
    label.pack(side = RIGHT)

    frame4 = Frame(self.win)
    frame4.pack()
    self.listbox = Listbox(frame4)
    self.listbox.insert(1, 'mode not selected!')
    self.listbox.insert(2, 'no UPS found')
    self.listbox.insert(3, 'I/O port not ready!')
    b4 = Button(frame4, text = 'ACK' , command = self.dialog)
    b4.pack(side = LEFT, padx = 5)
    self.listbox.pack(side = LEFT)


def update(self):
    data = self.ser.readline(self.ser.inWaiting())
    if len(data) > 2:
        self.d.set(data)
    self.win.after(100,self.update)

def run(self):
    self.makewindow()
    self.update()
    self.win.mainloop()

SerialViewer().run()

1 个答案:

答案 0 :(得分:0)

我认为您可能会对如何使用grid()经理感到困惑。

查看代码的结果,我发现事情是错开的。我认为您正在更改row和列`,而不仅仅是更改其中一个。

当您在窗口小部件上使用网格管理器时,网格放置仅与其所在的框架相关。您也可以在框架上使用相同的网格管理器。通过这种方式,您可以决定帧的去向,而不用担心帧的创建顺序。

当我们从不同的小部件中定义了3列和3行时,下面表示使用网格布局的框架。

在每个框中,您将看到在为该窗口小部件分配网格时每个窗口小部件的放置位置。请记住,如果每行/每列中至少有一个小部件,则下面只会保持其形状。如果不是,框架将自动调整大小以适合其内容。

因此,例如,如果我们将小部件放在(0,0),(1,1)和(2,2),那么框架将保持其3x3布局。如果我们只将小部件放在(0,1),(0,2)和说(1,2)中,那么框架现在只有2x2网格。

+------------------+------------------+------------------+
|                  |                  |                  |
|                  |                  |                  |
| (row=0, column=0)| (row=0, column=1)| (row=0, column=2)|
|                  |                  |                  |
|                  |                  |                  |
|                  |                  |                  |
+------------------+------------------+------------------+
|                  |                  |                  |
|                  |                  |                  |
| (row=1, column=0)| (row=1, column=1)| (row=1, column=2)|
|                  |                  |                  |
|                  |                  |                  |
|                  |                  |                  |
+------------------+------------------+------------------+
|                  |                  |                  |
|                  |                  |                  |
| (row=2, column=0)| (row=2, column=1)| (row=2, column=2)|
|                  |                  |                  |
|                  |                  |                  |
|                  |                  |                  |
+------------------+------------------+------------------+

请注意,有很多工具可以通过使用锚点和粘性来获得更精确的展示位置。 grid()经理是Tkinter中3位不同几何经理的最爱,但他们都有自己的用途。

以下是代码的简化版,以便更好地说明grid()经理如何设置小部件的格式。

from Tkinter import *
import tkMessageBox

class SerialViewer:
    def __init__(self, parent):
        self.win = parent
        self.makewindow()

    def makewindow (self):
        frame1 = Frame(self.win)
        frame1.grid(row = 0, column = 1)
        b1 = Button(frame1, text = "ON")
        b2 = Button(frame1, text = "OFF")
        b1.grid(row = 0, column = 0)
        b2.grid(row = 0, column = 1)

        frame2 = Frame(self.win)
        frame2.grid(row = 0, column = 0)
        self.text = Label(frame2, text = '# main  power relay:')
        self.text.grid(row = 0, column = 0)
        r1 = Radiobutton(frame2,text = 'on')
        r2 = Radiobutton(frame2,text = 'off')
        b3 = Button(frame2, text = 'send')
        b3.grid(row = 0, column = 3)
        r1.grid(row = 0, column = 1)
        r2.grid(row = 0, column = 2)

        self.v2 = StringVar()
        self.text2 = Label(frame2, text = '# main UPS connect:')
        self.text2.grid(row = 1, column = 0)
        r3 = Radiobutton(frame2,text = 'on')
        r4 = Radiobutton(frame2,text = 'off')
        b5 = Button(frame2, text = 'send')
        b5.grid(row = 1, column = 3)
        r3.grid(row = 1, column = 1)
        r4.grid(row = 1, column = 2)

        frame3 = Frame(self.win)
        frame3.grid(row = 1)
        self.d = StringVar()
        self.d.set('default')
        label = Label(frame3, textvariable = self.d, relief = RAISED)
        label.grid(row = 0)

        frame4 = Frame(self.win)
        frame4.grid(column = 1)
        self.listbox = Listbox(frame4)
        self.listbox.insert(1, 'mode not selected!')
        self.listbox.insert(2, 'no UPS found')
        self.listbox.insert(3, 'I/O port not ready!')
        b4 = Button(frame4, text = 'ACK')
        b4.grid(row = 0, column = 0)
        self.listbox.grid(row = 1, column = 0)


root = Tk()    
app = SerialViewer(root)
root.mainloop()